Swift:为UINavigationController工具栏添加工具栏按钮时出现问题

Swift:为UINavigationController工具栏添加工具栏按钮时出现问题,swift,uinavigationcontroller,Swift,Uinavigationcontroller,在我的UIViewController子类中有以下viewDidLoad覆盖,它嵌入在导航控制器中。我已经取消隐藏工具栏,当我运行时,工具栏就在那里(这确认了我在导航控制器中,并且我正确地对其进行了寻址),但我无法显示任何按钮。我做错了什么 override func viewDidLoad() { super.viewDidLoad() var buttons = [UIBarButtonItem]() for title in buttonTitleArray {

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

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(按钮,动画:false)添加按钮。但也不起作用。

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

    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

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

    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

您是尝试在导航栏上添加按钮(自定义/系统)还是在导航栏上设置工具栏?我尝试在窗口底部添加工具栏。如果对我添加的两个不同的按钮感到困惑,我会这样做,因为我以前没有使用过工具栏按钮,所以我想看看普通按钮是什么。换句话说,您可以忽略我为每个标题制作两个按钮的事实。您是尝试在导航栏上添加按钮(自定义/系统)还是在导航栏上设置工具栏?我尝试在窗口底部添加工具栏。如果对我添加的两个不同的按钮感到困惑,我会这样做,因为我以前没有使用过工具栏按钮,所以我想看看普通按钮是什么。换句话说,你可以忽略我为每个标题制作两个按钮的事实。谢谢。看起来我的问题是将按钮添加到navigationController.toolbarItems而不是self.toolbarItems谢谢。我的问题似乎是将按钮添加到navigationController.toolbarItems而不是self.toolbarItems