开发者

Trying to add a subview, drawRect isn't called

开发者 https://www.devze.com 2023-01-27 11:40 出处:网络
I\'m trying to add a subview to my main view. Here\'s the relevant code from my viewController: - (void)viewDidLoad {

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号