美文网首页
ios button 的偏移量

ios button 的偏移量

作者: Icy_Summer | 来源:发表于2018-11-23 11:22 被阅读0次

UIButton 默认图片在左,文字在右,按中华习惯大多时候需要交换位置,让文字在左,图片在右。

以下为对 titleEdgeInsets 和 imageEdgeInsets 两个属性的理解。

这两个属性是指他们相对于原来位置的偏移量

偏移量图示

以原来位置为参考,titleLab 向左偏移量为负数,长度为 imageView 的宽度,向右偏移为 正数,长度也是 imageView 宽度。

因此,button 的 titleEdgeInsets 属性设置为:

        CGFloat imgWidth =Btn.imageView.bounds.size.width; 

        [BtnsetTitleEdgeInsets:UIEdgeInsetsMake(0, - imgWidth,0, imgWidth )];

同理,imageEdgeInsets 属性设置为:

        CGFloat titleWidth =Btn.titleLabel.bounds.size.width;

        [BtnsetImageEdgeInsets:UIEdgeInsetsMake(0, titleWidth,0,- titleWidth)];

到此就完成了。

如果你需要 title 和 image 有间隔,只需在偏移量中考虑到加上间隔就可以了。 

tips: 你想偏移 x ,需要edgeInsets的左右都设置x,才能得到正确的便宜,附代码:

控制变量对比 运行结果

最简单的就是自定义button,想弄啥弄啥

相关文章

网友评论

      本文标题:ios button 的偏移量

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