0
votes

Xcode 10, Swift 5

I'm trying to create a button on the navigation bar but I can't get it to actually be displayed. The setup of my project is:

  1. ViewController1 (VC1, yellow): "Click" button that starts segue to
  2. NavigationController (NC): Segue to
  3. ViewController2 (VC2, red): "Next" button that loads
  4. ViewController3 (VC3, green)

Embedding VC2 in a UINavigationController and connecting it to VC3 with a segue automatically creates a "Back" button in VC3.

enter image description here

How do I make my "Button" on the right in the UINavigationBar in VC3 become visible - using only the Storyboard?

1

1 Answers

1
votes

The mistake I made was to add a UINavigationBar, which was added behind the existing UINavigationBar and because of this all of its items were invisible!

A UINavigationBar that was automatically added by a UINavigationController doesn't show up in the hierarchy! There'll be one if there's a "back" button, so in this case add a UINavigationItem instead. This creates a Title item.

Drag the new item you want to add over the left or right side of the UINavigationBar and there'll be a blue square, indicating where you can drop it. This item can be a UIBarButtonItem (which can be linked up like a button), a UIButton (which automatically also creates a UIBarButtonItem),... or even a UIView. Dropping an item adds it to the hierarchy and if it's the very first one, two new children are also going to be added to the Title:

  1. Left Bar Button Items
  2. Right Bar Button Items

enter image description here

Important: Adding something to the left "drop zone" overwrites the "back" button, which means that you're going to have to deal with going back to the previous screen yourself. This is easily accomplished by creating an "onClick" action for this item (ctrl+drag to the .swift file) and then calling:

navigationController?.popViewController(animated: true)

enter image description here