Change the color of the UIBarButtonItem font

I tried to change the font color of the panel element on the right to purple, but it still displays as white. I consulted with this question and question . How to fix it?

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 
+9
source share
4 answers

How about using:

 func setTitleColor(UIColor?, for: UIControlState) 

The documentation says that it sets the color of the header to use in the specified state.

 sortButton.setTitleColor( .red, for: .normal) 
+7
source

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 
+21
source

Please try this

 sortButton.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal) 
+4
source

It's simple, just create a link to the UIBarButtonItem from Main.stroyboard to the appropriate swift file, like this one,

 @IBOutlet var yourBarBtn: UIBarButtonItem! 

After that write this line,

 yourBarBtn.tintColor = .white //your_color 

This is it!

+2
source

Source: https://habr.com/ru/post/1269454/


All Articles