UISemanticContentAttribute: 如何将 UIButton 的图片移至右边

UISemanticContentAttribute: 如何将 UIButton 的图片移至右边

UIkit 的Button 预设是图片在左边

由于一些画面需要,可能需要调整图片的位置。可以透过一些设定图片边件数值得程式码,但是也可以透过设定一些更简单的参数调整。
image.png

UISemanticContentAttribute 语意内容属性

UISemanticContentAttribute(语意内容属性)是视图内容的语意描述,用于确定在从左到右和从右到左的布局之间切换时,视图是否应该翻转。

这个属性用于适应不同语言和文字方向的介面,以确保内容的呈现在不同的在地化环境中是正确的。 透过这个属性,iOS应用程式可以根据使用者的语言和本地化设定自动调整介面的方向,以确保文字和视图的布局是合适的。 这有助于确保应用程式在各种语言和文字方向下都能提供一致的使用者体验

public enum UISemanticContentAttribute : Int, @unchecked Sendable {    case unspecified = 0    case playback = 1    case spatial = 2    case forceLeftToRight = 3    case forceRightToLeft = 4}

unspecified 默认值,非特定的,当未明确指定语义属性时,系统将根据应用程式的本地化设定和使用者首选项来决定视图的方向。
playback 音乐播放器的分类。
spatial 地图或是图形。
forceLeftToRight 强制从左至右。
forceRightToLeft 强制从右至左。

forceLeftToRight

这边使用从右至左的属性,效果如下。

button.semanticContentAttribute = .forceRightToLeft

image.png

完整的程式码:

    let loginButton: UIButton = {        let button = UIButton(configuration: .gray())        button.setTitle("Login", for: .normal)        button.configuration?.cornerStyle = .capsule        button.configuration?.baseBackgroundColor = .lightGray        button.setImage(UIImage(systemName: "globe"), for: .normal)        button.semanticContentAttribute = .forceRightToLeft        button.translatesAutoresizingMaskIntoConstraints = false        return button    }()

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章