mootymoots
mootymoots

Reputation: 4575

UINavigationBar refuses to show in Modal View Controller

I am loading a Modal view controller using the following code in my RootViewController:

[self.navigationController presentModalViewController:accountViewController animated:YES];

In the accountViewController xib file, I have set a navigation bar. My MainWindow.xib and RootViewController.xib also have the navigation bar setup correctly. Additionally, my app delegate has setup the navigation controller (I assume) correctly:

UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;

[window addSubview:navigationController.view];

However, when I load my accountViewController the UINavigationBar is nowhere to be seen. Is it not possible to show a UINavigationBar in a modal view? I was planning to use it to hide the back button, and add a right button...

Upvotes: 3

Views: 4118

Answers (3)

Sabya
Sabya

Reputation: 21

You can also set the presentation style in the Attribute Inspector to "Current Context". Modal View will not cover the Navigational Bar.

Upvotes: 0

Shaggy Frog
Shaggy Frog

Reputation: 27601

sha's answer is correct, but I'm giving my own answer to expand on it with a code example to make it clear.

You probably want something like:

- (void)showAccountViewController
{
    AccountViewController* accountViewController = [[AccountViewController alloc] initWithNibName:@"AccountView" bundle:nil];
    ...
    // Initialize properties of accountViewController
    ...
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:accountViewController];
    [self.navigationController presentModalViewController:navController animated:YES];
    [navController release];
    [accountViewController release];
}

Upvotes: 6

sha
sha

Reputation: 17860

You need to push not viewController but navigationController that has viewController inside.

Upvotes: 1

Related Questions