我想为UISegmentedControl的文本和背景设置颜色。因此,我要设置四种颜色,即文本和背景的默认和选定颜色。
我可以使用tintColor
和backgroundColor
设置背景。但是如何设置默认文本颜色,而不是色调颜色?
Note:这是swift3,而不是较早的语言。我是ios的初学者,我只是从swift3开始,对以前的语言没有任何经验。
任何帮助将不胜感激。
UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()], forState: .Selected)
Swift 4.2 +代码来更新UISegmentedControl
的文本颜色(例如下面的sc
:]]
// selected option color
sc.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.green], for: .selected)
// color of other options
sc.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)
这里是可行的:
Swift 4.2 +
customSC.backgroundColor= UIColor(red: 0.5, greep: 0.5, blue: 0.5, alpha: 1.0)
customSC.tintColor= UIColor(red: 0.5, greep: 0.5, blue: 0.5, alpha: 1.0)