将UIButton标题右侧的图像与imageEdgeInsets和titleEdgeInsets对齐 [英] Aligning image on right side of title on UIButton with imageEdgeInsets and titleEdgeInsets

查看:78
本文介绍了将UIButton标题右侧的图像与imageEdgeInsets和titleEdgeInsets对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在标题的右侧添加一个图像,像这样

I would like to add an image on the right side of the title something like this

|Button  [image]|
|title          |

我可以通过这段代码来实现

And I am able to achieve this by this code

button.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0)
button.imageEdgeInsets = UIEdgeInsetsMake(0, 135, 0, 5)

但是,当标题较大时,标题标签和图像会相交.如何打破标题标签?

But when the title is large, the title label and image intersect with each other. How can I break the title label?

推荐答案

此扩展名对我有用:

扩展名:

extension UIButton {
  func imageToRight() {
      transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
      titleLabel?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
      imageView?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
  }
}

用法:

button.imageToRight()

这篇关于将UIButton标题右侧的图像与imageEdgeInsets和titleEdgeInsets对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆