Search code examples
iosswiftuisegmentedcontrol

Change Segment Control Title Color on segment switch in swift


i have a view controller on which i have segment control and i switch segments on swipe gesture, now i want that when i switch segments the current segment title color should become white and remaining color should get grey, i have searched about it but i got results for background color changing , how i can change segment control title color on switching between segments? This, is my code for segment on swipe,

 let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
    segmentControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
    segmentControl.fallBackToPreIOS13Layout(using: UIColor.clear)
    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedRight))
    swipeRight.direction = UISwipeGestureRecognizer.Direction.right
    self.activeView.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedLeft))
    swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
    self.closedView.addGestureRecognizer(swipeLeft)

 @objc func swipedRight(){
    segmentControl.selectedSegmentIndex = 0
    self.activeView.isHidden = false
    self.closedView.isHidden = true
    let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    segmentControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
    let titleText = [NSAttributedString.Key.foregroundColor: UIColor.gray]
    segmentControl.setTitleTextAttributes(titleText, for: .disabled)

    getActiveQuestionAPI()
}

@objc func swipedLeft(){
    segmentControl.selectedSegmentIndex = 1
    self.activeView.isHidden = true
    self.closedView.isHidden = false
    let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    segmentControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
    let titleText = [NSAttributedString.Key.foregroundColor: UIColor.gray]
    segmentControl.setTitleTextAttributes(titleText, for: .disabled)

    getCloseQuestionAPI()
}

Solution

  • For checking when segment control value changes you can use addTarget method like this.

    segmentControl.addTarget(self, action: #selector(onSegmentedControlValueChanged(_:)), for: .valueChanged)
    

    Then just implement onSegmentedControlValueChanged as you did with your previous methods like this:

    @objc func onSegmentedControlValueChanged(_ sender: UISegmentedControl) {
      // Do something when segment control value changes
    }
    

    For changing title text of your segment control you don't really have to check when value changes and you can achieve this just by below code snippet:

    let titleTextAttributesForSelected = [NSAttributedString.Key.foregroundColor: UIColor.white]
    let titleTextAttributesForNormal = [NSAttributedString.Key.foregroundColor: UIColor.black]
    segmentControll.setTitleTextAttributes(titleTextAttributesForSelected, for: .selected)
    segmentControll.setTitleTextAttributes(titleTextAttributesForNormal, for: .normal)
    

    This is all you need for changing segment control title color for different states.