25

I'm having a hard time adding a button to the toolbar in swift, below you can see an image of the toolbar that I'm after, unfortunately even though I have it designed in my Storyboard file, it doesn't show up when setting the toolbar to be visible.

The way that I have designed this is two items, the first being a flexable space element, and the second being an add element. It looks like this:

enter image description here

Here's the code that I've used to attempt to replicate this in code:

self.navigationController?.toolbarHidden = false
self.navigationController?.toolbarItems = [UIBarButtonItem]()
self.navigationController?.toolbarItems?.append(
    UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
)
self.navigationController?.toolbarItems?.append(
    UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:")
)

As you can see I'm setting the toolbar to be visible, initializing (and clearing) the toolbarItems array of UIBarButtonItem, and then adding two UIBarButtonItem's to the array, in the proper order.

However, the toolbelt remains empty, why is this?

6 Answers 6

46

None of the above worked for me, but:

Swift 3 / Swift 4

self.navigationController?.isToolbarHidden = false

var items = [UIBarButtonItem]()

items.append( UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil) )
items.append( UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(add)) ) // replace add with your function

self.toolbarItems = items // this made the difference. setting the items to the controller, not the navigationcontroller
Sign up to request clarification or add additional context in comments.

3 Comments

This works, but the real question is what are the rest of them doing?
Those who scroll down will be rewarded. Thank you.
This should be the accepted answer instead, but yes, what makes it different from the accepted answer?
24

The usual way to do that is to create the array of toolbar items and then assign the array to the items property of the toolbar.

self.navigationController?.isToolbarHidden = false
var items = [UIBarButtonItem]()
items.append(
    UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)
items.append(
    UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(onClickedToolbeltButton(_:)))
)
toolbarItems = items

3 Comments

If action is nil, why do you need to set target as self?
as @DavidSeek mentioned, self.toolbarItems on UIViewController is the right way to set toolbar buttons.
Oh, just know navigationController has a build-in toolbar, I created my own toolbar and set its constraints programmatically before, what a miss:)
7
self.navigationController?.toolbarItems = items

self.navigationController?.setToolbarItems(items, animated: false)

self.navigationController?.toolbar.setItems(items, animated: false)

Try it.

        self.navigationController?.toolbarHidden = false
        var items = [UIBarButtonItem]()
        items.append(
            UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil)
        )
        items.append(
            UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "onClickedToolbeltButton:")
        )

        self.navigationController?.toolbar.setItems(items, animated: false)

Comments

1

Here is an example with MKUserTrackingBarButtonItem:

navigationController?.toolbarHidden = false
let barButtonItem = MKUserTrackingBarButtonItem(mapView: self.mapView)
self.toolbarItems = [barButtonItem]

Comments

-2
let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "addSomething:")
toolbarItems = [UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: self, action: nil),addButton]
self.navigationController!.setToolbarHidden(false, animated: false)

Comments

-2

Updated answer using the current selector syntax for

Swift 3

var barButtons = [UIBarButtonItem]()
barButtons.append(
    UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(ThisViewController.onDoneBarButtonClick))
)
self.navigationItem.setRightBarButtonItems(barButtons, animated: false)

You can place this code in any loading event. It works seamless for me in viewDidLoad().

Replace "ThisViewController.onDoneBarButtonClick" with your view controller class name and any method you want to write to manage the toolbar button click.

Comments

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.