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,想弄啥弄啥
网友评论