Setting background color of UIView subclass doesn&

2019-06-18 23:07发布

I'm trying to change background color of one of my UIView subclasses. For some reason self.backgroundColor = [UIColor whiteColor];doesn't do anything when I put it in my - (id)initWithFrame:(CGRect)framemethod inside the view. The view is always black. I have also tried self.myView.backgroundColor ... from my view's controller, but that didn't work either. Any ideas on what I'm doing wrong?

The relevant code looks like this:

[...]
@interface PaperView : UIView
[....]

[...]
@implementation PaperView
[...] 

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [...]
        // Initialization code        
        self.backgroundColor = [UIColor whiteColor]; // This doesn't do anything, the view is always black.
    }
    return self;
}

7条回答
劫难
2楼-- · 2019-06-18 23:43

If this view is being unarchived from a xib, you need to override -initWithCoder:. -initWithFrame: is only invoked if you are creating your view programmatically.

查看更多
登录 后发表回答