8

I want to change the tintColor of UISegmentedControl selected segment in Swift 3. I've searched a lot of answers in Objective-c...
This is my code:

class ViewController:UIViewController{

var segment:UISegmentedControl

override func viewDidLoad() {
super.viewDidLoad()

segment.insertSegment(withTitle: "AAA", at: 0, animated: true)
segment.insertSegment(withTitle: "BBB", at: 1, animated: true)
segment.insertSegment(withTitle: "CCC", at: 2, animated: true)

segment.addTarget(self, action: #selector(changeValue), for: .valueChanged)
segment.selectedSegmentIndex = 0

view.addSubview(segment)

}
  func changeValue(sender:AnyObject) {

  //I don't know how to do that change color when segment selected
  // 

}

}

Thanks!

Paul Hunt
  • 3,395
  • 2
  • 24
  • 39
  • Here is a beautiful article to customize segmentedControl :) http://smnh.me/customizing-appearance-of-uisegmentedcontrol/ I believe you will have to subclass the UISegmentedControl change the background image for various states :) – Sandeep Bhandari Jan 09 '17 at 09:24
  • You can take reference from http://stackoverflow.com/questions/29312447/how-to-set-uisegmentedcontrol-tint-color-for-individual-segment – Keyur Hirani Jan 09 '17 at 09:27
  • @imbeginner_sorry pls check my answer and reply.. – KSR Jan 09 '17 at 09:29
  • just add tint color to the UISegmentControl like : segment.tintColor = UIColor.green – soumil Jan 09 '17 at 09:33
  • Please check the answers and reply! – Mr. Xcoder Jan 09 '17 at 09:46

6 Answers6

10

Use the code below for above ios 13,

if #available(iOS 13.0, *) {
        segment.selectedSegmentTintColor = .red
} else {
        segment.tintColor = .red
}
ajit.prazanna
  • 134
  • 2
  • 5
9

To programmatically change tint color of segment,

segment.tintColor = UIColor.yellow

Sneha
  • 2,200
  • 6
  • 22
  • 36
6

if you want to set the color of the title for example, you can do it like this:

let titleTextAttributes = [NSForegroundColorAttributeName: Color.blue]
segmentedControl.setTitleTextAttributes(titleTextAttributes, forState: .Selected)
2

In Main.storyboard, select segmentControl and change the property "Tint" as shown in below screenshot:

enter image description here

If you create the segmentedControl programmatically, then use this:

 segment.tintColor = UIColor.red
KSR
  • 1,699
  • 15
  • 22
1

A new property was added in iOS 13: selectedSegementTintColor. The old tintColor property no longer works in iOS 13.

You can find a more complete writeup of the changes here iOS 13 UISegmentedControl: 3 important changes

Kirby Todd
  • 11,254
  • 3
  • 32
  • 60
0

Add the following code to your changeValue function:

func changeValue(sender: UISegmentedControl){
for i in 0..<sender.subviews.count {
    if sender.subviews[i].isSelected() {
        var tintcolor = UIColor.red // choose the color you want here
        sender.subviews[i].tintColor = tintcolor
    }
    else {
        sender.subviews[i].tintColor = nil
    }
}
}

This is a swift version of the accepted answer from this question: UISegmentedControl selected segment color

Community
  • 1
  • 1
Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44