Search code examples
swiftborderuisegmentedcontrol

set specific border for each segment


I want to set a specific border color for selected segment and set a diffrence border color for each others. Here an example that I want to do it.

enter image description here

But I don't know how can I do it.


Solution

  • What you want to do is make use of UISegmentedControl's selectedSegmentTintColor. Setting this will make the selected segment a different color from all other segments.

    You can also get the UISegmentedControl.selectedSegmentIndex and use this index to manually change the color of the selected segment's border. The way you would do this is:

    @IBAction func segmentedControlValueChanged(_ sender: Any) {
        let selectedIndex = segmentedControl.selectedSegmentIndex
        segmentedControl.subviews[selectedIndex].layer.borderColor = UIColor.red.cgColor
    }