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:开发者_Go百科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?
In your subclass you should use the designated initialiser for UIView:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
//Implementation code...
}
}
Ok, figured it out. Wasn't setting the frame of my subView.
精彩评论