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

查看:27
本文介绍了如何将图像放在 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天全站免登陆