以编程方式将子视图添加到 NIB 的方法
What method to programmatically add subview to NIB
我正在 drawRect
中的 UIView
笔尖添加一个 UIButton
。
-(void)drawRect:(CGRect)rect {
self.button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[self.button setImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
[self.button setTintColor:[UIColor whiteColor]];
[self addSubview:self.button];
}
阅读此 post 后,它说每当修改视图框架时都会调用 drawRect
。我应该添加自定义 UI 元素的什么方法,或者我应该创建自己的方法并调用它。
通常我都是这样做的
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self)
{
[self load] ;
}
return self ;
}
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame] ;
if(self)
{
[self load] ;
}
return self ;
}
-(void)load{
//add your subviews here .
}
我正在 drawRect
中的 UIView
笔尖添加一个 UIButton
。
-(void)drawRect:(CGRect)rect {
self.button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[self.button setImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
[self.button setTintColor:[UIColor whiteColor]];
[self addSubview:self.button];
}
阅读此 post 后,它说每当修改视图框架时都会调用 drawRect
。我应该添加自定义 UI 元素的什么方法,或者我应该创建自己的方法并调用它。
通常我都是这样做的
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if(self)
{
[self load] ;
}
return self ;
}
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame] ;
if(self)
{
[self load] ;
}
return self ;
}
-(void)load{
//add your subviews here .
}