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、显示示例:

自定义按钮,是图片和文字水平居中显示

 

相关文章:

  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-10
  • 2022-12-23
  • 2022-02-09
猜你喜欢
  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2021-09-17
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
相关资源
相似解决方案