为什么设置 barItems 不起作用?

Why setting the barItems is not working?

我有一个 UIViewController,我将它嵌入到 UINavigationController 中。

我想在工具栏中显示一项(对于工具栏,我的意思是 this

这是我在 viewDidLoad 方法中的代码

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)))

而且我已经有了 buyPotato 方法

func buyPotato() {

    }

如您所见,我尝试使用 viewController 或 navigationController 来做到这一点,但它不起作用。

我只能看到屏幕底部的工具栏,但没有任何按钮。

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)

根据 所写的答案,这必须对您有效。

删除

self.setToolbarItems(barButtonItems, animated: true)

添加

self.toolbarItems = barButtonItems