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

- (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;
}

你可能感兴趣的:(iOS 任意布局UIButton中的图片与文字)