Swift - The Hierarchy of UINavigationController (Programmatically) 🎯

The hierarchy of UINavigationController

Hello everybody, we’ll talk about the UINavigationController today. 🤠

A UINavigationController allows you to manage a large number of view controllers in stack.

Let me explain you,

You have an application that have main, login and signup controller, that application starts running on main controller, you have two button to push login / signup controllers.

Yes, you can via UINavigationController!

How to setup project with navigation controller (Programmatically, without Storyboard)

1- You have to delete .storyboard file on your project.

2- As you can see in below, you have to remove storyboard name on Project Navigator > Select Project > General > Deployment Info > Main Interface

3- Let’s modify the AppDelegate file

We’ve defined our main controller (ViewController) then set as root view controller of UINavigationController.

It means, your navigation controller will be set of our application’s root controller (window.rootViewController).

Let’s start coding the mentioned application in above.

We have two buttons on main controller, you’ll be able to push login / register controllers via buttons.

If you pressed the login or register button on main controller, you’ll see that given the controllers as below.

You can reach project files on GitHub. 😎

Some tricks about UINavigationController

1- Dynamically changing a UINavigationController’s rootViewController

This is a good point to talk about.

Let’s explain, why we need to change Root View Controller?

Your users have successfully logged in to your application then terminated the application while already logged in. In the next time, you don’t want them to be able to encounter main or login controller. So, you’ll decide on AppDelegate when they launch your application, you can determine if they are logged in and set the rootViewController accordingly.

However, if they want to logoff?

In this case, you have to set their rootViewController as the main controller.

I’ll share my solution with you. In this extension, you’ll able to set new rootViewController. I suggest you follow the comment lines. I explained what they were doing there.

What is the UINavigationBar? 🤔

The UINavigationController adds the UINavigationBar to top of your view controllers. That’s optional, totally. You can hide it. Besides if you don’t want to hide, you can manage all items. I’ll explain in below.

The hierarchy of UINavigationBar

Also, UINavigationBar have some items, these are listed in below as you can see on the image.



You can back the your previous controller on your navigation stack.

There are some attributes to manage them, you can reach them on Apple Docs. backBarButtonItem by Apple Documentation


Located on the middle side of the Navigation Bar.


Located on the left side of the Navigation Bar.


Located on the right side of the Navigation Bar.

Thanks for reading 😛 , If you have any questions about the UINavigationController or related items. You can reach me on Twitter.

Spread the word 🧛‍