UINavigationBar refuses to show in Modal View Controller

寵の児 提交于 2019-12-07 14:32:48

问题


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.



来源:https://stackoverflow.com/questions/3690917/uinavigationbar-refuses-to-show-in-modal-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!