How to Add Buttons to Navigation Controller Visible After Segueing

How to add buttons to navigation controller visible after segueing?

For XCode 6, the UINavigationItem for the 2nd view controller onwards is not added automatically on the View Controller Object inside the storyboard. You will have to drag the UINavigationItem onto the Navigation Bar for that view Controller Object before adding UIBarButtonItem on top of it.

I am not sure why it is designed that way. I only discovered about this a few weeks ago.

Xcode 6 UiNavigationItem

How do you add a bar button item to all View Controllers in a Navigation Controller

There's a couple ways you could do it.


Make the SceneDelegate the delegate of the navigation controller then add the button to ever view controller shown.

func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
viewController.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Exit", style: .plain, target: self, action: #selector(exitPressed))
}

Make a view controller subclass that just sets the right bar button item. Then each screen that needs the button can subclass it.

class ExitButtonViewController: UIViewController {
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Exit", style: .plain, target: self, action: #selector(exitPressed))
}

func exitPressed() {
// add a common implementation or require the subclass to add one.
}
}

Make a protocol that can simplify adding the button in multiple view controllers.

protocol Exitable: NSObjectProtocol {
func exitPressed()
}

extension Exitable where Self: UIViewController {
func setupExitButton() {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Exit", style: .plain, target: self, action: #selector(exitPressed))
}
}

class SomeViewController: UIViewController, Exitable {
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
setupExitButton()
}

func exitTapped() {
// Do something
}
}

Adding buttons to navigation controllers

So how can I add buttons to the navigation controller, in another viewcontrollerS

Drag a Navigation Item into your third view controller. Now you can add bar button items to that.

Edit navigation bar in storyboard after segue

There are workaround

  1. just add new Navigation Controller for B

  2. write the Title and put any buttons in B Navigation Bar

  3. Delete the new Navigation Controller for B

Done

Navigation bar button item missing after segue style Replace

Try changing the Segue Style from Replace to Push. It should resolve your problem.

When you push a UIViewController on a UINavigationController, it will be added to a stack and you will be able to navigate back :

Your navigation controller stack before tapping "Detail":

Master

Your navigation controller after tapping detail and navigating a little more ...:

Master -> Detail (Navigation button is displayed) -> Some other details (Navigation button is displayed) -> And maybe some more (Navigation button is displayed)

When you call a new segue using Replace like you did, the Master will be replaced with Detail, and you will not be able to navigate back because Master was replaced and it's not on the stack anymore.

Your navigation controller stack before tapping "Detail":

Master

Your navigation controller after tapping detail using replace like you did:

Detail (no navigation button because master was replaced and we don't have nothing more on the stack)

Navigation bar missing in storyboard with show segue in Xcode 8

First, drag a navigation item under your second view controller:
Sample Image

Then, you have it to edit:
Sample Image

Xcode 8, adding UIButtons to NavigationBar for non initial view controllers

It's does not show, because your TabBarController also have his own UINavigationBar. ViewControllers are inside TabBarController

you can create custom TabBarController and handle tabs actions
Try this code:

class TabBarController: UITabBarController {
override var selectedViewController: UIViewController? {
didSet {
switch self.selectedViewController {
case self.selectedViewController is FirstViewController:
self.navigationItem.rightBarButtonItem = self.firstButton
case self.selectedViewController is SecondViewControlller:
self.navigationItem.rightBarButtonItem = self.secondButton
default:
break
}
}
}
}


Related Topics



Leave a reply



Submit