【问题标题】:How do I change the color of the unselected/normal segment text in an iOS 13 segmented controller?如何更改 iOS 13 分段控制器中未选择/正常分段文本的颜色?
【发布时间】:2020-08-13 15:17:04
【问题描述】:
我正在尝试配置UISegmentedController 类的实例,以便我可以将未选定段中的文本颜色从默认的黑色设置为两段分段控制器的白色。但我找不到任何方法。我尝试使用setTitleTextAttributes(_,for:)方法,但找不到合适的NSAttributedString.Key 属性来更改文本颜色。
【问题讨论】:
标签:
uisegmentedcontrol
nsattributedstringkey
【解决方案1】:
这在操场上对我有用:
import UIKit
import PlaygroundSupport
let seg = UISegmentedControl(items: ["foo", "bar"])
seg.setTitleTextAttributes([.foregroundColor: UIColor.red], for: .normal)
seg.setTitleTextAttributes([.foregroundColor: UIColor.blue], for: .selected)
seg.selectedSegmentIndex = 0
PlaygroundPage.current.liveView = seg