0

I have the following viewDidLoad override in my subclass of UIViewController that's embedded in a Navigation Controller. I have un-hidden the toolbar, and when I run, the toolbar is there (which confirms that I am inside a navigation controller and that I'm addressing it correctly), but I can't get any buttons to show. What am I doing wrong here?

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
}

I've tried adding the buttons using self.navigationController?.setToolbarItems(buttons, animated: false) but that doesn't work either.

2
  • Are you try to add buttons(Custom/Systems) on navigation bar OR setting a toolbar on navigation bar? Commented Feb 11, 2017 at 11:55
  • I'm trying to add a toolbar at the bottom of the window. If there's confusion about the two different buttons I'm adding, I'm doing that because I haven't used toolbar buttons before so I wanted to see what the plain one is. In other words, you can ignore the fact that I'm making two buttons for each title. Commented Feb 11, 2017 at 12:20

1 Answer 1

3

Please try below code to display toolbar buttons on view:-

    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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It looks like my problem was adding the buttons to navigationController.toolbarItems rather than self.toolBarItems

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.