Xcode自定义按钮
//设置frame
-(instancetype)initWithFrame:(CGRect)frame
{
if (self=[super initWithFrame: frame]) {
self.titleLabel.textAlignment=NSTextAlignmentCenter;
self.backgroundColor=[UIColor greenColor];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// self.titleLabel.textColor=[UIColor blueColor];
};
return self;
}
//更新子控件布局
-(void)layoutSubviews
{
[super layoutSubviews];
CGFloat width=self.frame.size.width;
CGFloat height=self.frame.size.height;
self.imageView.frame=CGRectMake(0, 0, width, width);
self.titleLabel.frame=CGRectMake(0, width, width, height-width);
// self.titleLabel.textColor=[UIColor purpleColor];
}
今天在自定义按钮的时候出现自定义按钮的文字一直显示不出来,捣鼓后发现是由于背景颜色和文字颜色同为白色造成的,在 initWithFrame:
方法中不能使用self.titleLabel.textColor=[UIColor blueColor];
设置颜色,可以使用[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]
对文字颜色进行设置;