1

我正在尝试将子视图添加到我的主视图中。这是我的 viewController 中的相关代码:

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

myView 中的 drawRect 不会被调用。

但是,如果我使用 MyUIViewSubclass 作为 viewController 的主视图(在 Interface Builder 中设置它),drawRect 会被调用。

我需要做什么才能在我的 subView 中调用 drawRect?

4

2 回答 2

7

在您的子类中,您应该为 UIView 使用指定的初始化程序:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
       //Implementation code...
    }
}
于 2010-11-28T05:32:53.597 回答
1

好的,想通了。没有设置我的子视图的框架。

于 2010-11-28T19:16:33.547 回答