morgancodes
morgancodes

Reputation: 25285

Trying to add a subview, drawRect isn't called

I'm trying to add a subview to my main view. Here's the relevant code from my viewController:

- (void)viewDidLoad {
    [super viewDidLoad];
    MyUIViewSubclass* myView = [[MyUIViewSubclass alloc] init]; 
    [self.view addSubview:myView]; // self.view is a simple UIView
    [myView setNeedsDisplay];
}

drawRect in myView doesn't get called.

However, if I use a MyUIViewSubclass as the main view for the viewController (setting it in Interface Builder), drawRect does get called.

What do I need to do to get drawRect called in my subView?

Upvotes: 1

Views: 2606

Answers (2)

morgancodes
morgancodes

Reputation: 25285

Ok, figured it out. Wasn't setting the frame of my subView.

Upvotes: 1

Rog
Rog

Reputation: 18670

In your subclass you should use the designated initialiser for UIView:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
       //Implementation code...
    }
}

Upvotes: 7

Related Questions