Search code examples
iosswiftuisegmentedcontrolviewdidload

segmented control color not showing when going back and fourth from view


my segmented control shows the purple text that I want when first being loaded, however when I press back on the navigation bar and then back into the view the color is gone. I have tried it in viewDidLoad and viewWillAppear.

   override func viewDidLoad() {

    mySegmentedControl.tintColor = UIColor.black
    mySegmentedControl.setFontSize(fontSize: 14)

    UISegmentedControl.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.purple], for: UIControlState.selected)

    super.viewDidLoad()


}

Solution

  • I think you should set setTitleTextAttributes for each UISegmentedControl instead of setting for all UISegmentedControl.

    override func viewDidLoad() {
        super.viewDidLoad()
    
        mySegmentedControl.tintColor = UIColor.black
        mySegmentedControl.setFontSize(fontSize: 14)
        mySegmentedControl.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.purple], for: .selected)
    }