美文网首页iOS 专题
Masonry设置UIbutton内边距兼容问题

Masonry设置UIbutton内边距兼容问题

作者: iOS_soft | 来源:发表于2019-04-04 11:29 被阅读0次

在使用masonry过程中为button设置约束时宽度自适应,但是一般button的title两边都会留白,一般最简单的做法是两边加空格。但是身为较真的我不想这样做,而且空格也不能满足UI对间距像素的要求。
最开始想了一个骚操作

[confirmButton.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_offset(18);
    make.bottom.mas_offset(-18);
 }];

但是这样在iOS10.0的版本里不兼容,导致标题不显示😭
然后想到了用

[confirmButton setTitleEdgeInsets:UIEdgeInsetsMake(18, 0, 18, 0)];
[confirmButton.titleLabel sizeToFit];

但是并不能解决问题。
最后、最后、最后!!!
想到了UIbutton一个被我们忽略的属性contentEdgeInsets //内边距
所以:

confirmButton.contentEdgeInsets = UIEdgeInsetsMake(18, 0, 18, 0);

完美解决,并且兼容所有版本😝

相关文章

网友评论

    本文标题:Masonry设置UIbutton内边距兼容问题

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