How to change the text color of rightBarButtonItem Item in Swift

I am trying to change the text color of the rightBarButtonItem item text and it blue by default . I am trying to change it to black .

My current code for that is:

self.navigationItem.rightBarButtonItem?.tintColor = .black

This does not change anything and the text color is the same . Added the cancel rightBarButtonItem image that i need to change the text from blue to black. How can i do that enter image description here

>Solution :

Make sure to add your code to your view controller’s viewWillAppear method and call setNeedsStatusBarAppearanceUpdate()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    navigationController?.navigationBar.tintColor = .black
    setNeedsStatusBarAppearanceUpdate() // don't forget to always call setNeedsStatusBarAppearanceUpdate when changing the button's tintColor
}

Leave a Reply