iOS 重写UIButton

创建一个类"DetailButton.h"继承自UIButton

@interface DetailButton : UIButton


- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {

        // 设置按钮的其他属性

        self.titleLabel.textAlignment = NSTextAlignmentLeft;

        self.titleLabel.font = [UIFont systemFontOfSize:16.0];

        [self setBackgroundColor:[UIColor clearColor]];

    }

    return self;

}


// 重写layoutSubviews方法,手动设置按钮子控件的位置

- (void)layoutSubviews {

    [super layoutSubviews];

    

    self.imageView.frame = CGRectMake(10, (self.frame.size.height-25)/2, 25, 25);

    self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.imageView.frame)+10, 0, self.frame.size.width-CGRectGetMaxX(self.imageView.frame)-10, self.frame.size.height);

}


//使用DetailButton

DetailButton *button = [DetailButton buttonWithType:UIButtonTypeCustom];




你可能感兴趣的:(iOS,iOS开发)