Matt McMinn
Matt McMinn

Reputation: 16291

UIView background color not set until device rotates

I'm working with a simple iPad application, and I've got a simple problem. I'm setting up a custom UIView; here's my initWithFrame:

- (id)initWithFrame:(CGRect)frame 
{
    if ((self = [super initWithFrame:frame])) 
    {
        self.backgroundColor = [UIColor colorWithWhite:.4 alpha:1.0];
        ....
    }

    return self;
}

The problem is that when the app starts, in this view the background color is not applied, even though the rest of the init is running (controls are added, etc). When I rotate the device though, the background color is applied, and will remain applied for the lifetime of the app. I think I'm missing a layout command somewhere, but I'm not sure where. Any ideas?

EDIT 2

Here's the call to init the view. These methods are in my ViewController.

-(id)initWithFrame:(CGRect)_rect 
{
    rect = _rect;
    if(self = [super init]) 
    {
        ......
    }
    return self;
}

- (void)loadView 
{
    [super loadView];

    myView = [[MyView alloc] 
                     initWithFrame:rect];

    self.view = myView;
}

Upvotes: 0

Views: 905

Answers (3)

jlehr
jlehr

Reputation: 15597

The -initWithFrame: is called by Interface Builder, but not by the NSCoder instance that unarchives your view from the nib file at runtime. Instead, the unarchiving mechanism calls -initWithCoder:, so your view subclass would need to do something like this:

- (id)initWithCoder:(NSCoder *)decoder
{
    if ((self = [super initWithCoder:decoder]))
    {
        self.backgroundColor = [UIColor purpleColor];

        // Do other initialization here...
    }

    return self;
}

Upvotes: 3

Vinzius
Vinzius

Reputation: 2901

Mmmm

If you don't have the call method (wich alloc your UIView) we won't be able to help you.

It can be because your uiview frame isn't set up correctly. Or because you don't call initWithFrame after alloc but you cal init function.

So give us more information if you want be helped :-)

Good luck !

Upvotes: 0

Nevin
Nevin

Reputation: 7809

Try to change the background in the viewDidLoad method?

Upvotes: 0

Related Questions