iOS UIButton 图片文字左右互移 位置对调 解决方案

实现类似效果:

代码实现:

 btnGrade.titleEdgeInsets = UIEdgeInsetsMake(0, -(btnGrade.imageView?.bounds.width)!, 0, (btnGrade.imageView?.bounds.width)!)
 btnGrade.imageEdgeInsets = UIEdgeInsetsMake(0, (btnGrade.titleLabel?.bounds.width)!+8, 0, -(btnGrade.titleLabel?.bounds.width)!)

说明:

1、UIEdgeInsetsMake的left和right都要设置,如果只设一个,是无效的

2、代码的+8  是为了中间的间隔,更美观,这个可自由设置,也可以在title里设置一样的。

3、设置便宜时,如果有需要设置按钮文字大小等属性,一定要在此之前设置好,不然获取的titleLabel的bounds是不准的。

原文地址:https://www.cnblogs.com/yajunLi/p/6805315.html