I want to set colors for the text and background of UISegmentedControl. So I've four colors to set, the default and selected color of the text and background.
I can use tintColor
and backgroundColor
to set the background. But how to set the default text color, not same as the tint color?
Note: here is swift3 not the older language. I'm a beginner of ios, I just start from the swift3, have no experience of the former language.
Any help will be appreciated.
To change the color/font of the selected segment titles, use setTitleTextAttributes with a state of . selected / UIControlStateSelected . If you create a segmented control with images, if the images are created as template images, then the segmented control's tintColor will be used to color the images.
The easiest workaround is create dummy labels in IB, give them the text the color you like and set to hidden. You can then reference this color in your code to set your label to the desired color. The only way I could change the text color programmatically was by using the standard colors, UIColor.
Swift 5+ code to update text color for your UISegmentedControl
(sc
in this example)
// selected option color
sc.setTitleTextAttributes([.foregroundColor: UIColor.green], for: .selected)
// color of other options
sc.setTitleTextAttributes([.foregroundColor: UIColor.white], for: .normal)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With