Kri*_*ofe 5 iphone uisegmentedcontrol ios10 xcode8 swift3.0.2
我想为UISegmentedControl的文本和背景设置颜色。因此,我要设置四种颜色,即文本和背景的默认和选定颜色。
我可以使用tintColor和backgroundColor设置背景。但是如何设置默认的文本颜色,而不是色调颜色?
注意:这是swift3而非较旧的语言。我的ios的初学者,我刚刚从swift3开始,没有前者语言的经验。
任何帮助将不胜感激。
Dav*_*nte 11
斯威夫特 4.2 +
segmentedControl.tintColor = .black //background
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .selected)
segmentedControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .normal)
Run Code Online (Sandbox Code Playgroud)
Swift 4代码来更新您的文本颜色UISegmentedControl(sc在下面的示例中):
// selected option color
sc.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.green], for: .selected)
// color of other options
sc.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
Run Code Online (Sandbox Code Playgroud)
UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()], forState: .Selected)
Run Code Online (Sandbox Code Playgroud)
这是可行的一个:
//default background color
customSC.backgroundColor = UIColor(red:65/255.0, green:140/255.0, blue:218/255.0, alpha: 1.0)
//selected background color
customSC.tintColor = UIColor(red:65/255.0, green:140/255.0, blue:218/255.0, alpha: 1.0)
//selected title text color
customSC.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: UIControlState.selected)
//default title text color
customSC.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.darkGray], for: UIControlState.normal)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3986 次 |
| 最近记录: |