Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting background color of UIView subclass doesn't work

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;
}
like image 785
Anders Avatar asked Dec 28 '11 19:12

Anders


1 Answers

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.

like image 194
Mark Adams Avatar answered Nov 15 '22 07:11

Mark Adams