Search code examples
iosswiftuinavigationcontrolleruitoolbar

Add a button to the native toolbar


I have the following code

final class MyViewController: UIViewController, 
    UICollectionViewDataSource, UICollectionViewDelegate, /*..*/ {

    //
    // Declaring local variables, other controllers and delegates etc.
    //

    override func viewDidLoad() {
        super.viewDidLoad()

        let item = UIBarButtonItem(title: "A button that should do nothing", 
            style: .Plain, target: nil, action: nil)

        self.navigationController!.toolbar.items = [item]

        self.navigationController!.toolbarHidden = false
    }

    // the rest of my code
}

And no matter what I do, the toolbar appears, yet nothing will render inside of it.

I also tried

self.navigationController?.toolbarItems?.insert(item, atIndex: 0)

to do the insertion of the item, but still no luck.

What am I doing wrong?


Solution

  • Fixed by adding items via

    self.toolbarItems = [item]