3

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 navigationController, but it doesn't work.

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

amagain
  • 2,042
  • 2
  • 20
  • 36
sarah
  • 1,201
  • 1
  • 9
  • 29

2 Answers2

4
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.

Community
  • 1
  • 1
amagain
  • 2,042
  • 2
  • 20
  • 36
1

Delete

self.setToolbarItems(barButtonItems, animated: true)

Add

self.toolbarItems = barButtonItems
brycejl
  • 1,411
  • 17
  • 22