快速如何设置UISegmentedControl的默认文本颜色?

问题描述 投票:7回答:5

我想为UISegmentedControl的文本和背景设置颜色。因此,我要设置四种颜色,即文本和背景的默认和选定颜色。

我可以使用tintColorbackgroundColor设置背景。但是如何设置默认文本颜色,而不是色调颜色?

Note:这是swift3,而不是较早的语言。我是ios的初学者,我只是从swift3开始,对以前的语言没有任何经验。

任何帮助将不胜感激。

iphone swift3 xcode8 ios10 uisegmentedcontrol
5个回答
7
投票
UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.redColor()], forState: .Selected)

9
投票

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)

5
投票

这里是可行的:


4
投票

Swift 4.2 +


1
投票
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)
© www.soinside.com 2019 - 2024. All rights reserved.