Skip to content

UINavigationController: beforeshow event ​

The beforeshow event fires when the navigation controller is about to show a view controller, as the transition begins.

Syntax ​

nav.addEventListener('beforeshow', (event) => { })

Event object ​

Event, with one extra field:

FieldTypeMeaning
viewControllerUIViewControllerThe view controller about to be shown

The type definitions don't yet have a type for this event object; annotate the viewController field yourself, as shown in the example.

Not cancelable, and it can't be used to stop the user from going back.

When it fires ​

  • Fires when code pushes a view controller; also fires when the user taps the back button or swipes back, and both cases receive the same event (measured). The event object has no field telling "code or user" or "push or pop" apart; when you need to tell, compare nav.viewControllers.length before and after the transition.
  • Whether it fires when code calls the popViewController() family of methods is not verified in the simulator.
  • The callback doesn't run synchronously at the moment of the transition; it runs a little later. See How the runtime works.

To tell which view controller is about to be shown, use event.viewController, not nav.topViewController.

Examples ​

js
const home = new UIViewController();
const nav = new UINavigationController(home);
nav.addEventListener('beforeshow', (ev) => {
  const event = /** @type {Event & { viewController: UIViewController }} */ (ev);
  nav.setNavigationBarHidden(event.viewController === home, { animated: true });
});

Effect: The home view controller has no navigation bar; the navigation bar appears when you go to another view controller and disappears again when you return home. The acceptance tests' interface does exactly this.

Corresponding iOS API ​

navigationController:willShowViewController:animated: on UINavigationControllerDelegate. nav.delegate isn't exposed.

See also ​