Reputation: 550
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.
Upvotes: 3
Views: 9625
Reputation: 6795
a) Make a new BarButtonItem with System Item
, Target View Controller Instance
and Action Method Name
.
b) Assign it to leftBarButtonItem
c) Good to do this in func viewWillAppear()
And thats it !
let editButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(actionEdit)) // actionEdit is your Action Method for this button
navigationItem.leftBarButtonItem = editButtonItem
Upvotes: 0
Reputation: 3008
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)
Upvotes: 1