Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add UINavigationBar in an UIViewController?

I have an UIViewController class(Say it is XXX). I present this view controller as modally by the code..

XXX *xxx = [ [XXX alloc] init];
[self presentModalViewController:xxx animated:YES];
[xxx release];

I want to add a navigation bar on the top of the XXX view. So I used UINavigationBar object in XXX's loadView method.

UINavigationBar *navBar = [ [UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:navBar];
[navBar release];

But, It throws an error as "EXC_BAD_ACCESS". Any help...?

Thanks

like image 519
Confused Avatar asked Mar 15 '12 11:03

Confused


People also ask

How do I customize my navigation bar?

From Settings, tap Display, and then tap Navigation bar. Make sure Buttons is selected, and then you can choose your desired button setup at the bottom of the screen. Note: This option will also affect the location you swipe when using Swipe gestures.

How do I add a navigation bar in Swift storyboard?

Go to the Storyboard. Select the View Controller and in The Editor menu select Embed in -> Navigation Controller. Next, drag a Bar Button from the Object Library to the left side of the Navigation Bar and name it "Left Item". Repeat this for the right side and name it "Right Item".


1 Answers

OPTION-1:

Try adding navigation bar from the XIB of viewController called XXX.

OPTION-2:

Add a UINavigationController and present it modally.

Replace your code :

XXX *xxx = [[XXX alloc] init];
[self presentModalViewController:xxx animated:YES];
[xxx release];

with this code:

XXX *xxx = [[XXX alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigation animated:YES];
[navigation release];

Hope this helps you.

like image 200
Parth Bhatt Avatar answered Oct 02 '22 00:10

Parth Bhatt