views:

49

answers:

3

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;
}
A: 

Try to change the background in the viewDidLoad method?

Nevin
A: 

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 !

Vinzius
A: 

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;
}
jlehr
I'm not using a nib file, I'm building it programatically.
Matt McMinn