我现在正在将 UIButton
添加到 drawRect
中的 UIView
Nib 。
-(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 ,它表示每当修改 View 框架时都会调用 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 .
}
关于ios - 以编程方式将 subview 添加到 NIB 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40699892/