- (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中的图片与文字
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 前言 之前对于那种上面是图片下面是文字的按钮都是通过一个ImageView、一个Label、一个Button实现的...
- 1、只有文字(默认居中) 1.1 文字居左显示 代码实现: 1.2 文字居右显示 代码实现: 2、只有图片(默认居...
- 文/白朵朵 1. 感觉一夜之间我的朋友圈里突然出现了好多的土豪啊!!一个个全都是年度影响力人物,年支出都有过了千八...