UINavigationBar refuses to show in Modal View Cont

2019-05-14 19:03发布

问题:

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...

回答1:

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];
}


回答2:

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



回答3:

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