Swift:无法为UINavigationController工具栏添加工具栏按钮

时间:2017-02-11 10:54:33

标签: swift uinavigationcontroller

我的viewDidLoad子类中有以下UIViewController覆盖,它嵌入在导航控制器中。我没有隐藏工具栏,当我运行时,工具栏就在那里(它确认我在导航控制器中并且我正确地解决了它),但是我无法显示任何按钮。我在这里做错了什么?

override func viewDidLoad() {
    super.viewDidLoad()

    var buttons = [UIBarButtonItem]()
    for title in buttonTitleArray {
        let plainButton = UIBarButtonItem(title: title, style: .plain, target: self, action: #selector(self.setContentMode(_:)))
        let systemButton = UIBarButtonItem(barButtonSystemItem: .play, target: self, action: #selector(self.setContentMode(_:)))
        buttons.append(plainButton)
        buttons.append(systemButton)
    }
    self.navigationController?.toolbarItems = buttons
    self.navigationController?.isToolbarHidden = false
}

我尝试使用self.navigationController?.setToolbarItems(buttons, animated: false)添加按钮,但这也不起作用。

1 个答案:

答案 0 :(得分:1)

请尝试以下代码在视图上显示工具栏按钮: -

    var items = [UIBarButtonItem]()

    //Custom Button
    let plainButton = UIBarButtonItem(title: "Test", style: .Plain, target: self, action: #selector(self.customButtonTapped))
    items.append(plainButton)

    //Add Flexible space between buttons
    let flexibalSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
    items.append(flexibalSpace)

    //Toolbar system button
    let systemButton = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: #selector(self.systemButtonTapped))
    items.append(systemButton)

    self.toolbarItems = items //Display toolbar items.
    self.navigationController?.toolbarHidden = false