美文网首页
自定义UIButton重写layoutSubviews重新布局文

自定义UIButton重写layoutSubviews重新布局文

作者: ios_stand | 来源:发表于2017-04-04 16:05 被阅读0次
#import <UIKit/UIKit.h>

@interface XMGSqaureButton : UIButton

@end
#import "XMGSqaureButton.h"

@implementation XMGSqaureButton
- (void)setup
{
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.titleLabel.font = [UIFont systemFontOfSize:15];
    [self setBackgroundImage:[UIImage imageNamed:@"mainCellBackground"] forState:UIControlStateNormal];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setup];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 调整图片
    self.imageView.y = self.height * 0.15;
    self.imageView.width = self.width * 0.5;
    self.imageView.height = self.imageView.width;
    self.imageView.centerX = self.width * 0.5;
    
    // 调整文字
    self.titleLabel.x = 0;
    self.titleLabel.y = CGRectGetMaxY(self.imageView.frame);
    self.titleLabel.width = self.width;
    self.titleLabel.height = self.height - self.titleLabel.y;
}

相关文章

网友评论

      本文标题:自定义UIButton重写layoutSubviews重新布局文

      本文链接:https://www.haomeiwen.com/subject/ajtzottx.html