UISegmentedControl changing the segment's image tint

Hi All,

Very new to Swift here. I have a very simple UISegmentedControl where I have set the image for each segment.

segmentedControl.setImage(UIImage(named: “cat”), forSegmentAt: 0)
segmentedControl.setImage(UIImage(named: “dog”), forSegmentAt: 1)

Now that I have the image set, I wanted to change the tintColor to blue for the selected segment and gray for the unselected segment. Below is the code that I was using.

for i in 1…segmentedControl.subviews.count {
let index = i - 1
segmentedControl.subviews[index].tintColor = ( segmentedControl.selectedSegmentIndex == index ? UIColor.blue : UIColor.gray)
}

The problem I am experiencing is that the selected segment’s image is always white. I am not sure why this is even the case as I did not specify the tintColor as white. Can anyone see what I am doing wrong? Thanks.

John

I am also experiencing the same issue, trying since a day… Any solution you got?

Hi @mubashir, one suggestion I have would to go to Image Set in the assets folder and setting the images to “Original Image” under the Render As option. Then afterwards set the tint color on the segment control.

1 Like

Hi @jbreen,
Hope that suggestion helped resolve your issue.

There is another thing to note, as a suggestion whenever you use any UI Component and would like some sort of customization, it is better to setup the tint at the start.

Secondly, how are you calling that loop that sets the tintColor?

cheers,

Hi @lewis-h,
why would you change the tint to red, when the original question want to tint then blue or grey.

I know this is an older topic but I ran into this same thing today. If you are using custom images, set your tint to clear. That’ll allow the full image to come through. Also do what was recommended in the assets folder setting the images to “Original Image”. If you don’t want to mess with having to adjust and render each image, create an asset of 28x28. That’ll fit right within the Segmented control original size.