iOS - ScrollView更改背景

问题描述 投票:0回答:1

我正在尝试更改我创建的自定义滚动视图的背景但由于某种原因它不会改变。这是代码:

@implementation ProfileScrollView
@synthesize changePictureButton;
@synthesize changePictureLabel;
@synthesize nameLabel;
@synthesize genderLabel;
@synthesize ageLabel;
@synthesize writeAboutYourselfLabel;
@synthesize interestsLabel;
@synthesize interestFrame;
@synthesize isDrawn;
- (void)drawRect:(CGRect)rect
{

    self.backgroundColor = [UIColor redColor];
    self.contentMode = UIViewContentModeRedraw;

    if (self.isDrawn == NO)
    {
        self.changePictureLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:14];
        self.nameLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:14];
        self.ageLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:14];
        self.genderLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:14];
        self.writeAboutYourselfLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:14];
        self.interestsLabel.font = [UIFont fontWithName:@"OpenSans" size:14];

        self.changePictureLabel.textColor = [UIColor colorWithRed:0.314 green:0.89 blue:0.761 alpha:1];

        self.interestFrame = self.interestsLabel.frame;

        int startHeight = 0;

        NSMutableArray *interests = [[NSMutableArray alloc] initWithObjects:@"Game of Thrones", @"Starcarft", @"Eating out", @"Soccer", @"Football", @"Video Games", nil];

        CGRect screenRect = [[UIScreen mainScreen] bounds];
        CGFloat screenWidth = screenRect.size.width;

        int currentWidth = 0;
        for (NSString *interest in interests) {

            CGSize buttonSize = [interest sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"OpenSans-Light" size:14]}];

            if (currentWidth == 0)
            {
                currentWidth = self.interestFrame.origin.x;
                startHeight = self.interestFrame.origin.y + self.interestFrame.size.height + 5;
            }
            else if (currentWidth > (screenWidth - 30))
            {
                currentWidth = self.interestFrame.origin.x;
                startHeight += (buttonSize.height + 15);
            }

            CGRect frame = CGRectMake(currentWidth, startHeight, buttonSize.width + 15, buttonSize.height + 10);
            CustomButton *aButton = [[CustomButton alloc] initWithFrame:frame];
            [aButton setTitle:interest forState:UIControlStateNormal];
            aButton.buttonColour = [UIColor whiteColor];
            aButton.titleLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:14];
            aButton.enabled = NO;
            [self addSubview:aButton];
            aButton = nil;

            currentWidth += buttonSize.width + 20;
        }

        CGSize buttonSize = [@"Add +" sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"OpenSans-Light" size:14]}];

        currentWidth = self.interestFrame.origin.x;
        startHeight += buttonSize.height + 20;

        CGRect frame = CGRectMake(currentWidth, startHeight, buttonSize.width + 15, buttonSize.height + 10);
        CustomButton *addButton = [[CustomButton alloc] initWithFrame:frame];
        [addButton setTitle:@"Add +" forState:UIControlStateNormal];
        addButton.titleLabel.font = [UIFont fontWithName:@"OpenSans-Light" size:14];
        [addButton addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:addButton];

        self.isDrawn = YES;
    }
}


@implementation MyProfileViewController
@synthesize saveButton;
@synthesize scrollView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollView.navController = self.navigationController;
}

当我隐藏滚动视图时,我设置为背景的imageview正确显示但是一旦我取消隐藏滚动视图,背景就是黑色。有任何想法吗?

ios uiscrollview
1个回答
1
投票

解决了这个家伙。

我在xib文件上设置了Opaque。

© www.soinside.com 2019 - 2024. All rights reserved.