Changing font color of UIBarButtonItem

Viewed 17333

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
5 Answers

Its simple, just create a reference for UIBarButtonItem from Main.stroyboard to corresponding swift file like this,

@IBOutlet var yourBarBtn: UIBarButtonItem!

After that write this line,

yourBarBtn.tintColor = .white //your_color

Thats it!

First, you need to set plain properties then after you can write as like.

enter image description here

  @IBOutlet weak var btnGenerate: UIBarButtonItem!

  btnGenerate.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.colorBlue], for: .normal)
Related