自定义按钮,是图片和文字水平居中显示
2016-09-22 11:16 PingKang 阅读(923) 评论(0) 编辑 收藏 举报1、创建继承自UIButton的的子类
2、重写initWithFrame方法
3、关键代码
- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, frame.size.height - frame.size.width, 0); self.titleEdgeInsets = UIEdgeInsetsMake(frame.size.width,-frame.size.width,0,0); self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; return self; }
4、调用示例:
//免提按钮 self.speakerBtn = [[ChatTouchBtn alloc] initWithFrame:CGRectMake(paddingM +CGRectGetMaxX(self.pickDownBtn.frame), MaxHEIGHT/2 - (WIDTHWITH(70) + 30) - bottomF, WIDTHWITH(70), WIDTHWITH(70) + 30)]; [self.speakerBtn setImage:[UIImage imageNamed:@"speakerw@2x"] title:@"免提" forState:UIControlStateNormal]; [self.speakerBtn setImage:[UIImage imageNamed:@"speakerb@2x"] title:@"免提" forState:UIControlStateSelected]; [self.speakerBtn addTarget:self action:@selector(speakerAction:) forControlEvents:UIControlEventTouchUpInside]; [self.bottomView addSubview:self.speakerBtn];
5、显示示例: