Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add bar button item programmatically at ViewControllers after rootView Swift 3.0

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:

enter image description here

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:

enter image description here

any help would be appreciated.

like image 960
tara tandel Avatar asked Nov 22 '25 16:11

tara tandel


1 Answers

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)
like image 193
Devang Tandel Avatar answered Nov 25 '25 09:11

Devang Tandel