How to add UINavigationController Programmatically

by Amit Singh on December 6, 2011

UINavigationController is a subclass of UIViewController, but unlike UIViewController it’s not usually meant for you to subclass. This is because navigation controller itself is rarely customized beyond the visuals of the nav bar.

An instance of UINavigationController can be created either in code or in an XIB file with relative ease. It’s thought of as a stack: it has a root view controller, and then new view controllers can be pushed onto the stack (often when the user taps on a row in a table) or popped off the stack (often by pressing the back button).

The root view controller can be set in an XIB by dragging the view controller under the navigation controller, or in code by using initWithRootViewController when you create it.

The Navigation Stack

Four methods are used to navigate user through the stack:

  • pushViewController:animated:
  • popViewControllerAnimated:
  • popToRootViewControllerAnimated:
  • popToViewController:animated:

Generally you want animated set to YES, especially if it’s in response to a user action. I’ve seen some apps that don’t animate transitions between view controllers, and I always find it distracting.

You don’t have to keep track of the UINavigationController instance among all the view controllers on the stack. UIViewController has a property called navigationController which will return the associated navigation controller for the view controller. If it doesn’t have one, navigationController returns nil.

Enable UINaviogationController in ViewController Project:

open AppDelegate.m and locate the application:didFinishLaunchingWithOptions method. Inside application:didFinishLaunchingWithOptions,add following code.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}

Add Navigation Title

Just few lines of code enable you to add navigation bar title. Please find below that code.

self.navigationItem.title=@"Navigation Controller Example";

Adding Buttons in NavigationBar

Just 2 lined of code to enable you add UIBarButtons in UINavigationBar.

Back button in UINavigationBar

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
      style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

Left or Right button in UINavigationBar

 UIBarButtonItem *addNewContact=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewContact)];
// set LeftSide leftBarButtonItem
 self.navigationItem.rightBarButtonItem=addNewContact;
OR
// set rightSide leftBarButtonItem
  self.navigationItem.rightBarButtonItem=addNewContact;

Left or Right button in UINavigationBar

 UIBarButtonItem *addNewContact=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(addNewContact)];
    self.navigationItem.rightBarButtonItem=addNewContact;

Set Image in UIBarButton in UINavigationBar

 UIBarButtonItem *addNewContact=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"a.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(addNewContact)];
    self.navigationItem.rightBarButtonItem=addNewContact;

Push ViewController through NavigationController

DetailsViewController *detailsViewController = [[DetailsViewController alloc]
    initWithNibName:@"DetailsViewController" bundle:nil];
  detailsViewController.navigationItem.title = person.name;
  [self.navigationController pushViewController:detailsViewController];

Hiding Navigation Bar at the Root Level

Since the view controller at the root often doesn’t have a left or right button, it’s not too uncommon to want the navigation bar hidden when the root view controller is visible, but to appear when another view controller gets pushed on the stack. You should probably only do this when a title really isn’t necessary.

It can be a little tricky to get this to look right without the navigation bar appearing or disappearing oddly as you push or pop the stack. The place to do it is in the viewWillAppear: and viewWillDisappear: methods of the view controller:

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];

	[self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];

	[self.navigationController setNavigationBarHidden:NO animated:YES];
}

Related posts:

{ 2 comments }

Pooja Singh December 6, 2011 at 1:55 PM

I want to add UINavigationBar with presentationModalViewController.Can you guide us?
Pooja Singh recently posted..Siri can start and unlock your carMy Profile

Amit Singh December 6, 2011 at 1:57 PM

Thanks Pooja,
Sure, I will help you. Tomorrow, I will post new article regarding presentationModalViewController with UINavigationBar.
Amit Singh recently posted..Asus Transformer PrimeMy Profile

Comments on this entry are closed.