Search code examples
swiftuibuttonuinavigationbaruibarbuttonitemuicolor

Changing font color of UIBarButtonItem


I tried to change the font color of the right bar button item to purple, but it still shows up as white. I've consulted this question and this question. How do I fix this?

Code

let sortButton = UIButton(frame: CGRect(x: 0, y: 0, width: 34, height: 15))
    sortButton.setTitle("SORT", for: .normal)
    sortButton.titleLabel?.tintColor = UIColor.myMusicPurple
    sortButton.tintColor = UIColor.myMusicPurple        
    navigationItem.rightBarButtonItem =  UIBarButtonItem(customView: sortButton)
    navigationItem.rightBarButtonItem?.tintColor = UIColor.myMusicPurple

Solution

  • What about using:

    func setTitleColor(UIColor?, for: UIControlState)
    

    Documentation says it sets the color of the title to use for the specified state.

    sortButton.setTitleColor( .red, for: .normal)