How to change the colors of a segment in a UISegmentedControl in iOS 13?
As of iOS 13b3, there is now a selectedSegmentTintColor
on UISegmentedControl
.
To change the overall color of the segmented control use its backgroundColor
.
To change the color of the selected segment use selectedSegmentTintColor
.
To change the color/font of the unselected segment titles, use setTitleTextAttributes
with a state of .normal
/UIControlStateNormal
.
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. But this has a problem. If you set the tintColor
to the same color as selectedSegmentTintColor
then the image won't be visible in the selected segment. If you set the tintColor
to the same color as backgroundColor
, then the images on the unselected segments won't be visible. This means your segmented control with images must use 3 different colors for everything to be visible. Or you can use non-template images and not set the tintColor
.
Under iOS 12 or earlier, simply set the segmented control's tintColor
or rely on the app's overall tint color.
How to set UISegmentedControl Tint Color for individual segment
This will solve your problem:
var subViewOfSegment: UIView = mySegmentedControl.subviews[0] as UIView
subViewOfSegment.tintColor = UIColor.blueColor()
You can also
(mySegmentedControl.subviews[0] as UIView).tintColor = UIColor .blueColor()
How to change selected segment tintColor of UISegmentedControl in Swift
To programmatically change tint color of segment,
segment.tintColor = UIColor.yellow
UISegmentedControl selected segment color
I found A Simple Way to Add Color for Selected Segment in UISegmentcontrol
sender is UISegmentControl
for (int i=0; i<[sender.subviews count]; i++)
{
if ([[sender.subviews objectAtIndex:i]isSelected] )
{
UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0];
[[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
}
else
{
[[sender.subviews objectAtIndex:i] setTintColor:nil];
}
}
Check its Working For Me
Customise font color of each segment in UISegmented Control
One idea would be to recursively traverse through segment's view hierarchy and check if you encounter a UILabel
Set up your UISegmentControl
as normal
func addControl() {
let items = ["One", "Two", "Three"]
let segmentedControl = UISegmentedControl(items: items)
segmentedControl.frame = CGRect(x: 35, y: 200, width: 250, height: 50)
segmentedControl.center = view.center
segmentedControl.selectedSegmentIndex = 1
view.addSubview(segmentedControl)
// custom function
updateSegmentTextColor(segmentedControl)
}
This function runs through the view hierarchy and set's the color of the labels
// Just for testing
func randomColor() -> UIColor
{
let red = CGFloat(arc4random_uniform(256)) / 255.0
let blue = CGFloat(arc4random_uniform(256)) / 255.0
let green = CGFloat(arc4random_uniform(256)) / 255.0
return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}
private func updateSegmentTextColor(_ rootView: UIView)
{
for subview in rootView.subviews
{
if let label = subview as? UILabel
{
// Any color you want
label.textColor = randomColor()
}
updateSegmentTextColor(subview)
}
}
The result:
A couple of final notes:
- This solution could impact other tint properties you may want to set and is useful when you want to keep the same color of the text for selected and deselected states
- Future iOS updates and implementations of Segment Control could have an impact on how this works
Update
If you want to specify the title color for a specific segment, you could assume (gamble) that the segment control should lay out its subviews in the logical order.
I emphasized on assume because this order is not in our control and you are at the mercy of the implementation.
Infact, if you set the selected index segmentedControl.selectedSegmentIndex = 1
and if you have 4 segments, the order of laying out the subviews is 0, 2, 3, 1 so the selected segment gets added last.
What could work for your situation is as follows:
// Add a color queue to hold the colors for the 4 segments
var colorQueue: [UIColor] = [.red, .blue, .green, .orange]
private func updateSegmentTextColor(_ rootView: UIView)
{
for subview in rootView.subviews
{
if let label = subview as? UILabel,
!colorQueue.isEmpty
{
// Dequeue the front of the queue
let color = colorQueue.removeFirst()
label.textColor = color
}
updateSegmentTextColor(subview)
}
}
private func addControl() {
let items = ["One", "Two", "Three", "Four"]
let segmentedControl = UISegmentedControl(items: items)
segmentedControl.frame = CGRect(x: 35, y: 200, width: 250, height: 50)
segmentedControl.center = view.center
segmentedControl.tintColor = .blue
view.addSubview(segmentedControl)
// custom function
updateSegmentTextColor(segmentedControl)
// Add the selected index if you need AFTER the updates
segmentedControl.selectedSegmentIndex = 1
}
You get this
Trying to set different text color for each segment in UISegmentedControl
To create unique background color for selected segment you need to create action for UISegmentControl
and then based on the index set the color.
@IBAction func segmentChanged(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
sender.selectedSegmentTintColor = UIColor.red
} else {
sender.selectedSegmentTintColor = UIColor.blue
}
}
Edit:
So you can try to work with subviews of UISegmentControl
like that:
@IBOutlet weak var segment: UISegmentedControl! {
didSet {
for (index, view) in segment.subviews.enumerated() {
if index == 0 {
view.subviews.forEach{ label in
(label as! UILabel).textColor = UIColor.red
}
} else {
view.subviews.forEach{ label in
(label as! UILabel).textColor = UIColor.blue
}
}
}
}
}
Customizing the colors of a UISegmentedControl
UISegmentedControl has a tintColor property -- this allows you to change what color the control is, but not the general "style" (the rounded, beveled shape):
segmentedControl.tintColor = [UIColor blueColor];
As for creating UIImages on the fly, you can create a CGContext, do whatever drawing you need to in that context (including strings), and then get a UIImage out of the context's CGImage:
CGContextRef drawContext = CGBitmapContextCreate(<many parameters>);
//do drawing here
CGImageRef finalImage = CGBitmapContextCreateImage(drawContext);
UIImage *cellImage = [UIImage finalImage];
Please note, that if you use code like UIView.appearance().tintColor = .myColor
(or equiv. in ObjC), the effect most likely won't take place.
how to change the tint color of disable segment in UISegmentedControl
Check the following code:
1.It assigns green colour to selected segment.(segment-2)
2.Assigns blue colour to those not selected but enabled(segment-1 and 3).
3.Assigns lightGray colour for segments which are disabled.(segment-4)
-(IBAction)segmentedTapped:(UISegmentedControl*)sender{
for(int i=0;i<[sender.subviews count];i++)
{
if ([[sender.subviews objectAtIndex:i]isEnabled])
{
if([[sender.subviews objectAtIndex:i]isSelected])
{
UIColor *tintcolor=[UIColor greenColor];
[[sender.subviews objectAtIndex:i] setTintColor:tintcolor]; //sets green color for selected segment which is enabled
}
else
{
[[sender.subviews objectAtIndex:i] setTintColor:[UIColor blueColor]]; //sets blue colour for remaining segments which are enabled but not selected.
}
}
else
{
[[sender.subviews objectAtIndex:i] setTintColor:[UIColor lightGrayColor]];//sets ight gray for disabled segment.
}
}
}
The result will be:
if you want more information you can get from here:
UISegmentedControl selected segment color
Related Topics
Why Can't I Divide Integers in Swift
Expression Implicitly Coerced from 'String' to Any
Using Guard with a Non-Optional Value Assignment
Weak VS Unowned in Swift. What Are the Internal Differences
The File "Xxx.Mp4" Couldn't Be Opened Because You Don't Have Permission to View It
Open File Dialog Crashes in Swift
Why Does Swift Provide Both a Cgrect Initializer and a Cgrectmake Function
How to Reset/Restart Viewcontroller in Swift
How to Override Layout of Nstableheaderview
Can Swift Playgrounds See Other Source Files in the Same Project
How to Check Object Is Nil or Not in Swift
Swift: Popover Dismiss Callback
Swiftui .Rotationeffect() Framing and Offsetting
Differences Between "Static Var" and "Var" in Swift
My Data Changes in Uitableviewcell When I Scroll Down and Get Back
How to Apply Borders and Corner Radius to Uibarbuttonitem
How to Open Safari Extension Toolbaritem Popover Programmatically