Unable to add UIBarButtonItem to toolbar

After going through each stackoverflow solution for this problem, it still disappoints 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 to display a button on a toolbar - all I get is an empty toolbar. Is there something obvious I'm missing here?

+6
source share
4 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 is the point at which the UINavigationController receives the UIViewController toolbar items that it controls.

+5
source

using

 self.toolbarItems=[NSArray arrayWithObject:button1] 
+3
source

For those looking for a version of Swift , try the following:

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

The documentation of the UINavigationController.toolbar properties explicitly explains which API should be used to configure toolbar elements:

The contents of these toolbars are managed using custom view controllers associated with this navigation controller. . For each view controller in the navigation stack, you can assign a custom set of toolbar items using the setToolbarItems:animated: UIViewController .

- Link to the UINavigationController class

0
source

With Swift 3 / iOS 10, in the simplest case, when your navigation controller will contain only one view controller, you can use the code below to display the view controller using a toolbar containing a panel button element:

 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 the navigation controller stack, you will need to call the setToolbarHidden(_:animated:) UINavigationController method in viewWillAppear() and viewWillDisappear() to correctly display or hide the built-in navigation controllers in the 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) } } 
0
source

Source: https://habr.com/ru/post/953755/


All Articles