Allen
Allen

Reputation: 3771

Can't add UIBarButtonItem to toolbar

After going through every single stackoverflow solution for this problem, it's still frustratingly not working for me.

//UIBarButtonItem declaration
UIBarButtonItem* button1 = [[UIBarButtonItem alloc] initWithTitle:@"Button Text" 
style:UIBarButtonItemStyleBordered target:self action:@selector(myAction)];

//method 1
[self setToolbarItems:[NSArray arrayWithObjects: button1, nil] animated:YES];

//method 2
[self.navigationController.toolbar setItems:[NSArray arrayWithObject:button1]];

//method 3
self.navigationController.toolbarItems = [NSArray arrayWithObject:button1];

//displaying toolbar
[self.navigationController setToolbarHidden:NO];

None of the above methods work for displaying a button on the toolbar - all I get is a blank toolbar. Is there something obvious I'm missing here?

Upvotes: 8

Views: 3166

Answers (4)

Imanou Petit
Imanou Petit

Reputation: 92479

With Swift 3 / iOS 10, in the simplest case where your navigation controller will contain only one view controller, you may use the code below in order to display your view controller with a toolbar that contains a bar button item:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Show navigation controller’s built-in toolbar
        navigationController?.setToolbarHidden(false, animated: false)

        // Set the view controller toolbar items
        let items = [UIBarButtonItem(title: "Button Text", style: .plain, target: nil, action: nil)]
        setToolbarItems(items, animated: false)
    }

}

However, if you plan to have several view controllers in your navigation controller's stack, you will have to call UINavigationController's setToolbarHidden(_:animated:) method in viewWillAppear() and viewWillDisappear() in order to properly show or hide the navigation controller’s built-in toolbar:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set the view controller toolbar items
        let items = [UIBarButtonItem(title: "Button Text", style: .plain, target: nil, action: nil)]
        setToolbarItems(items, animated: false)
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Show navigation controller’s built-in toolbar
        navigationController?.setToolbarHidden(false, animated: false)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        // Hide navigation controller’s built-in toolbar
        navigationController?.setToolbarHidden(true, animated: false)
    }

}

Upvotes: 1

Paulo Mattos
Paulo Mattos

Reputation: 19339

For those looking for a Swift version, try this:

let someVC: UIViewController = ...
let someButton: UIBarButtonItem = ...
someVC.setToolbarItems([someButton], animated: true)

The UINavigationController.toolbar property documentation explicitly clarifies which API should be used for setting toolbar items:

Management of this toolbar’s contents is done through the custom view controllers associated with this navigation controller. For each view controller on the navigation stack, you can assign a custom set of toolbar items using the setToolbarItems:animated: method of UIViewController.

-- UINavigationController Class Reference

Upvotes: 0

Kirsteins
Kirsteins

Reputation: 27335

Move

//UIBarButtonItem declaration
UIBarButtonItem* button1 = [[UIBarButtonItem alloc] initWithTitle:@"Button Text" 
style:UIBarButtonItemStyleBordered target:self action:@selector(myAction)];

//method 1
[self setToolbarItems:[NSArray arrayWithObjects: button1, nil] animated:YES];

//displaying toolbar
[self.navigationController setToolbarHidden:NO];

to viewDidAppear:(BOOL)animated this is the point where UINavigationController get toolbar items of UIViewController that it manages.

Upvotes: 5

R A Khan
R A Khan

Reputation: 187

use

self.toolbarItems=[NSArray arrayWithObject:button1]

Upvotes: 3

Related Questions