【问题标题】:Why setting the barItems is not working?为什么设置 barItems 不起作用?
【发布时间】:2016-09-25 15:37:09
【问题描述】:

我有一个 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 来做到这一点,但它不起作用。

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

【问题讨论】:

    标签: ios swift uinavigationcontroller uitoolbar uitoolbaritem


    【解决方案1】:
    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)
    

    根据here 写的答案,这必须为您工作。

    【讨论】:

    • 哦,我根本不是在讨论导航栏,我是在讨论位于屏幕底部的工具栏
    • 请立即查看答案@sarah
    • 根据您的评论编辑!
    • 非常感谢
    【解决方案2】:

    删除

    self.setToolbarItems(barButtonItems, animated: true)
    

    添加

    self.toolbarItems = barButtonItems
    

    【讨论】:

      猜你喜欢
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 2014-12-08
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多