UISemanticContentAttribute: 如何将 UIButton 的图片移至右边
UIkit 的Button 预设是图片在左边
由于一些画面需要,可能需要调整图片的位置。可以透过一些设定图片边件数值得程式码,但是也可以透过设定一些更简单的参数调整。
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
完整的程式码:
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 }()