以下代码运行时无法显示Button:
- (id)initWithFrame:(CGRect)frame {
self = [superinitWithFrame:frame];
if (self) {
//初始化属性
[selfinitProperty];
//创建组件
[selfcreateComponent];
}
return self;
}
- (void)initProperty
{
self.backgroundColor =UIColorFromRGB_hex(0xffffff);
}
- (void)createComponent
{
_btnConfirm = [UIButtonbuttonWithType:UIButtonTypeCustom];
[_btnConfirmsetTitle:@"OK"forState:UIControlStateNormal];
[_btnConfirmsetBackgroundColor:UIColorFromRGB_hex(0xff00ff)];
[_btnConfirmaddTarget:selfaction:@selector(btnConfirmClicked)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:_btnConfirm];
}
- (void)viewDidLoad {
[_btnConfirmsetFrame:CGRectMake(10,self.bounds.origin.y +100, self.bounds.size.width -20, 40)];
}
将
- (void)viewDidLoad {
[_btnConfirmsetFrame:CGRectMake(10,self.bounds.origin.y +100, self.bounds.size.width -20, 40)];
}
改为
- (void)layoutSubviews {
[_btnConfirmsetFrame:CGRectMake(10,self.bounds.origin.y +100, self.bounds.size.width -20, 40)];
}
即可。viewDidLoad中什么都不需要做。