iOS - ScrollView更改背景

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS - ScrollView更改背景相关的知识,希望对你有一定的参考价值。

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

@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正确显示但是一旦我取消隐藏滚动视图,背景就是黑色。有任何想法吗?

答案

解决了这个家伙。

我在xib文件上设置了Opaque。

以上是关于iOS - ScrollView更改背景的主要内容,如果未能解决你的问题,请参考以下文章

更改 ScrollView SwiftUI 的背景颜色

iOS大标题不适用于scrollView后面的背景视图

AlertDialog 更改片段中的背景颜色 [重复]

使用 Kotlin 更改片段中的按钮背景

用scrollview更改工具栏文本颜色

如何从片段更改 Tablayout 的背景颜色?