您的位置:首页 > 产品设计 > UI/UE

自定义一个按钮,使按钮为上面图片下面文字

2017-03-23 10:46 357 查看

一:自定义一个UIButton

二:自定义的UIButton的.m文件中(复制我这的代码需要自己写一个UIView简化位置和宽高的拓展类,也可点击查看我的博客:“如何简化UIView的位置和宽高”)

- (void)setup

{

    self.titleLabel.textAlignment =
NSTextAlignmentCenter;

}

- (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.x =
0;

    self.imageView.y =
0;

    self.imageView.width =
self.width;

    self.imageView.height =
self.imageView.width;

    

    // 调整文字

    self.titleLabel.x =
0;

    self.titleLabel.y =
self.imageView.height;

    self.titleLabel.width =
self.width;

    self.titleLabel.height =
self.height -
self.titleLabel.y;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios uibutton