Usage of iOS Navigation Bar
The navigation bar contains buttons for navigation controller navigation. The title in the navigation bar is the title of the current view controller.
Example Code and Steps
Create a View Application
Now, select the application
Delegate.h
, add the navigation controller property as shown below:#import <UIKit/UIKit.h> @class ViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) ViewController *viewController; @property (strong, nonatomic) UINavigationController *navController; @end
Update the
application:didFinishLaunchingWithOptions:
method in theAppDelegate.m
file to allocate the navigation controller and make it the root view controller of the window, as shown below:- (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]; //Navigation controller init with ViewController as root UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = navController; [self.window makeKeyAndVisible]; return YES; }
Now, add a new class file
TempViewController
by selecting File -> New -> File... -> Objective-C Class, and name the classTempViewController
as a subclass ofUIViewController
.Add
navButton
inViewController.h
as shown below:// ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIButton *navButton; } @end
Now add the method
addNavigationBarItem
and call the method inviewDidLoad
Create the method for the navigation item
We also need to create another method to navigate to another view controller
TempViewController
.The updated
ViewController.m
is shown below:// ViewController.m #import "ViewController.h" #import "TempViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self addNavigationBarButton]; //Do any additional setup after loading the view, typically from a nib } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
-(IBAction)pushNewView:(id)sender {
TempViewController *tempVC = [[TempViewController alloc]
initWithNibName:@"TempViewController" bundle:nil];
[self.navigationController pushViewController:tempVC animated:YES];
}
-(IBAction)myButtonClicked:(id)sender {
// toggle hidden state for navButton
[navButton setHidden:!navButton.hidden];
}
-(void)addNavigationBarButton {
UIBarButtonItem *myNavBtn = [[UIBarButtonItem alloc] initWithTitle:
@"MyButton" style:UIBarButtonItemStyleBordered target:
self action:@selector(myButtonClicked:)];
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
[self.navigationItem setRightBarButtonItem:myNavBtn];
// create a navigation push button that is initially hidden
navButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[navButton setFrame:CGRectMake(60, 50, 200, 40)];
[navButton setTitle:@"Push Navigation" forState:UIControlStateNormal];
[navButton addTarget:self action:@selector(pushNewView:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:navButton];
[navButton setHidden:YES];
}
@end
Now when we run the application, we will get the following output.
Click the MyButton navigation button to toggle the visibility of the navigation button.
Click the navigation button to display another view controller, as shown below.