swiftuinavigationcontrolleruinavigationbaruinavigationitembackbarbuttonitem

How can I change the backBarButtonItem colour in Swift?


I am trying to change the default backBarButtonItem colour to white colour,

my codes look like below

self.navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()

But It is not working for me, any ideas?


Solution

  • If you want to change the backBarButton do this :

    override func viewDidLoad() {
    
        super.viewDidLoad()
    
        let newBackButton = UIBarButtonItem(title: "B",
        style: UIBarButtonItem.Style.plain, target: self, action: "backAction")
            navigationController?.navigationBar.topItem?.backBarButtonItem = newBackButton
        newBackButton.tintColor = .red
        // Do any additional setup after loading the view.
    }
    
    func backAction() -> Void {
        self.navigationController?.popViewController(animated: true)
    }
    

    image of code

    See the result:
    see the result