iOS 任意布局UIButton中的图片与文字

来源:互联网 发布:阿里云华北2 编辑:程序博客网 时间:2024/06/15 20:44
- (void)setup{    self.titleLabel.textAlignment = NSTextAlignmentCenter;}- (instancetype)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) {        [self setup];    }    return self;}- (void)layoutSubviews{    [super layoutSubviews];    // 调整图片    self.imageView.width = 20;    self.imageView.height = self.imageView.width;    self.imageView.x = (self.width - self.imageView.width) / 2;    self.imageView.y = (self.height - self.imageView.width) / 2 - 10;    // 调整文字    self.titleLabel.x = 0;    self.titleLabel.y = self.imageView.bottom + 5;    self.titleLabel.width = self.width;    self.titleLabel.textAlignment = NSTextAlignmentCenter;    self.titleLabel.height = 20;}
原创粉丝点击