Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 276
Alex Avatar asked Jul 03 '17 20:07

Alex


2 Answers

This should do the trick (if you have plain text)

let rightBarButtonItem = UIBarButtonItem(title: "Some text", style: .plain, target: self, action: #selector(someAction))
rightBarButtonItem.tintColor = UIColor.myMusicPurple

navigationItem.rightBarButtonItem = rightBarButtonItem
like image 50
Kristijan Delivuk Avatar answered Sep 20 '22 10:09

Kristijan Delivuk


Please try this

sortButton.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
like image 33
Avinash Avatar answered Sep 18 '22 10:09

Avinash