如何将图像放在UIButton中文本的右侧? [英] How do I put the image on the right side of the text in a UIButton?

查看:322
本文介绍了如何将图像放在UIButton中文本的右侧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可以避免,我不想使用子视图。我想要一个 UIButton ,里面有背景图片,文字和图片。现在,当我这样做时,图像位于文本的左侧。背景图片,文字和图片都有不同的高光状态。

I don't want to use a subview if I can avoid it. I want a UIButton with a background image, text, and an image in it. Right now, when I do that, the image is on the left side of the text. The background image, text, and image all have different highlight states.

推荐答案

尽管一些建议的答案非常具有创造性和极端聪明,最简单的解决方案如下:

Despite some of the suggested answers being very creative and extremely clever, the simplest solution is as follows:

button.semanticContentAttribute = UIApplication.shared
    .userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft

就这么简单。作为奖励,图片将位于从右到左区域的左侧。

As simple as that. As a bonus, the image will be at the left side in right-to-left locales.

编辑:因为问题已被提出有几次,这是 iOS 9 +

EDIT: as the question has been asked a few times, this is iOS 9 +.

这篇关于如何将图像放在UIButton中文本的右侧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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