sarah sarah - 6 months ago 22
iOS Question

why setting the barItems is not working?

I have a UIViewController, and I embed it into a UINavigationController.

I want to show one item in the toolbar (and by toolbar, I mean this:

This is my code in viewDidLoad method

self.navigationController?.toolbarHidden = false
self.navigationController?.toolbar.items?.append(UIBarButtonItem(title: "Buy Potato", style: .Plain, target: self, action: #selector(ViewController.buyPotato)))
self.navigationController?.toolbarItems?.append(UIBarButtonItem(title: "Buy Potato", style: .Plain, target: self, action: #selector(ViewController.buyPotato)))


self.toolbarItems?.append(UIBarButtonItem(title: "Buy Potato", style: .Plain, target: self, action: #selector(ViewController.buyPotato)))


and I already have the method buyPotato

func buyPotato() {

}


as you see, I tried to do that using either the viewController or the navigationControlelr, but no works.

All I can see is the toolbar at the button of my screen but without any button

Answer
self.navigationController?.toolbarHidden = false
var items = [UIBarButtonItem]()
items.append(
UIBarButtonItem(barButtonSystemItem: .Plain, target: self, action: nil))
items.append(
UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:"))
self.setToolbarItems(barButtonItems, animated: true)

This has to work for you as per the answer written here.