presentViewController 和显示导航栏 [英] presentViewController and displaying navigation bar
问题描述
我有一个视图控制器层次结构,最顶层的控制器显示为模态,想知道如何在使用时显示导航栏
I have a view controller hierarchy and the top-most controller is displayed as a modal and would like to know how to display the navigation bar when using
'UIViewController:presentViewController:viewControllerToPresent:animated:completion'
'presentViewController:animated:completion:' 的文档注释:
The docs for 'presentViewController:animated:completion:' note:
'在 iPhone 和 iPod touch 上,呈现的视图始终是全屏的.在 iPad 上,演示文稿取决于modalPresentationStyle 属性.'
'On iPhone and iPod touch, the presented view is always full screen. On iPad, the presentation depends on the value in the modalPresentationStyle property.'
对于modalPresentationStyle",文档说:
For 'modalPresentationStyle', the docs say:
呈现风格决定了模态呈现的视图控制器在屏幕上的显示方式.在 iPhone 和 iPod touch 上,模态视图控制器总是全屏显示,但在 iPad 上有几种不同的显示选项.
The presentation style determines how a modally presented view controller is displayed onscreen. On iPhone and iPod touch, modal view controllers are always presented full-screen, but on iPad there are several different presentation options.
一旦视图控件显示自身,有没有办法确保导航栏在状态栏下方可见?我应该将文档解释为,您没有 iPhone/iPod 的任何选项,只能在 iPad 上使用吗?
Is there way to ensure that the navigation bar is visible below the status bar once the view control displays itself? Should I interpret the doc as, you don't get any options of iPhone/iPod and only on iPad?
以前,我使用 'UIViewController:presentModalViewController:animated'
效果很好,但从 iOS 5.0 开始,API 已被弃用,所以我要切换到新的.
Previously, I was using 'UIViewController:presentModalViewController:animated'
which worked fine, but since iOS 5.0, the API has been deprecated so I'm switching over to the new one.
从视觉上看,我想要做的是让新控制器从屏幕底部滑入,就像以前的旧 API 一样.
Visually, what I'm looking to do is have the new controller slide in from the bottom of the screen, just like the old API used to do.
[使用代码更新]:
// My root level view:
UIViewController *vc = [[RootViewController alloc]
initWithNibName:nil
bundle:[NSBundle mainBundle]];
navController = [[UINavigationController alloc] initWithRootViewController:vc];
....
// Within the RootViewController, Second view controller is created and added
// to the hierarchy. It is this view controller that is responsible for
// displaying the DetailView:
SecondTierViewController *t2controller = [[SecondTierViewController alloc]
initWithNibName:nil
bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:t2controller animated:YES];
// Created by SecondTierViewController
DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil
bundle:[NSBundle mainBundle]];
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
controller.modalPresentationStyle = UIModalPresentationCurrentContext;
[self.navigationController presentViewController:controller
animated:YES
completion:nil];
推荐答案
确实,如果你在 iPhone 上以模态呈现一个视图控制器,无论你在顶视图控制器上如何呈现它总是全屏呈现导航控制器或任何其他方式.但您始终可以使用以下解决方法显示导航栏:
It is true that if you present a view controller modally on the iPhone, it will always be presented full screen no matter how you present it on the top view controller of a navigation controller or any other way around. But you can always show the navigation bar with the following workaround way:
而不是模态地呈现该视图控制器以模态方式呈现导航控制器,其根视图控制器设置为您想要的视图控制器:
Rather than presenting that view controller modally present a navigation controller modally with its root view controller set as the view controller you want:
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:myViewController];
//now present this navigation controller modally
[self presentViewController:navigationController
animated:YES
completion:^{
}];
当您的视图以模态呈现时,您应该会看到一个导航栏.
You should see a navigation bar when your view is presented modally.
这篇关于presentViewController 和显示导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!