【发布时间】:2013-01-02 14:56:23
【问题描述】:
我正在尝试做一些非常基本的事情。我想向我的 UIView 子类添加一个子视图。我假设我会将它放在下面的 initWithFrame 方法中,但是作为此类实例的视图不会绘制此子视图。我究竟做错了什么?
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
redView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
[redView setBackgroundColor:[UIColor redColor]];
[self addSubview:redView];
}
return self;
}
BTW redView 是定义在子类头部的一个属性,比如:
@property (strong, nonatomic) UIView *redView;
感谢阅读!
【问题讨论】:
-
代码看起来不错。尝试将 self.backgroundColor 设置为绿色以查看它是否显示。
-
实际上 initWithFrame 不会触发。我正在将情节提要中的元素分配给这个子类...
-
你如何分配初始化那个视图?
标签: ios objective-c uiview