Senseful
Senseful

Reputation: 91761

UIBarButtonItem added to storyboard does not appear at runtime

When I try to add a UIBarButtonItem to my UIViewController, nothing shows up at run time.

What I did:

  1. I started off with a brand new single view project.
  2. I dragged a UIBarButtonItem into the view controller.
  3. I can now see and customize the item in the storyboard, but when I run it, there is no toolbar.

Note: the Bar Button Item appears as a direct child of View Controller.

How can I get the UIToolbar to appear?

Upvotes: 2

Views: 2478

Answers (1)

Senseful
Senseful

Reputation: 91761

Adding toolbar items as direct children of the UIViewController corresponds to the toolbarItems property of a view controller. The documentation states:

If this view controller is embedded inside a navigation controller interface, and the navigation controller displays a toolbar, this property identifies the items to display in that toolbar.

So, you must do the following:

  1. Embed the UIViewController in a UINavigationController (e.g. select the UIViewController in the storyboard, choose Editor > Embed In > Navigation Controller).
  2. Select the UINavigationController and check the box that says Shows Toolbar (this is the similar to calling self.navigationController.toolbarHidden = NO in the view did load method).

Optionally, if you want to restore the behavior where the navigation bar wasn't visible, then uncheck the Shows Navigation Bar property.

An alternative approach is to not use the toolbarItems property, and instead add your own toolbar and maintain it yourself (e.g. add an IBOutlet and interact with it that way).

Upvotes: 5

Related Questions