Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 384
Allen Avatar asked Sep 13 '13 07:09

Allen


3 Answers

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.

like image 60
Kirsteins Avatar answered Nov 15 '22 15:11

Kirsteins


use

self.toolbarItems=[NSArray arrayWithObject:button1]
like image 39
R A Khan Avatar answered Nov 15 '22 16:11

R A Khan


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)
    }

}
like image 1
Imanou Petit Avatar answered Nov 15 '22 14:11

Imanou Petit