I have a View hierarchy
there is just one navigation controller and its rootViewController.
I want to add a left bar button item at the view which is circled.
the View I want to add barButton:

I tried
self.navigationController?.navigationItem.leftBarButtonItem = editButtonItem
in viewDidLoad() and viewWillAppear() but none of them worked out.
I also tried adding UINavigationItem in storyBoard and then adding barButtonItem. this didn't work too.
EDIT
I tried the answers below and that's what I get in the output.
my output after trying answers:

any help would be appreciated.
You can add Bar Button like
let btnleft : UIButton = UIButton(frame: CGRect(x:0, y:0, width:35, height:35))
btnleft.setTitleColor(UIColor.white, for: .normal)
btnleft.contentMode = .left
btnleft.setImage(UIImage(named :"yourimage.png"), for: .normal)
btnleft.addTarget(self, action: #selector(YOUR_ACTION), for: .touchDown)
let backBarButon: UIBarButtonItem = UIBarButtonItem(customView: btnleft)
self.navigationItem.setLeftBarButtonItems([backBarButon], animated: false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With