presentViewController并显示导航栏

Jon*_*ner 87 iphone uiviewcontroller modalviewcontroller presentmodalviewcontroller ios

我有一个视图控制器层次结构,最顶层的控制器显示为模态,并想知道如何在使用时显示导航栏

'UIViewController:presentViewController:viewControllerToPresent:animated:completion'
Run Code Online (Sandbox Code Playgroud)

'presentViewController的文档:动画:完成:'注意:

'在iPhone和iPod touch上,呈现的视图始终为全屏.在iPad上,演示文稿取决于modalPresentationStyle属性中的值.

对于'modalPresentationStyle',文档说:

演示样式确定如何在屏幕上显示模态呈现的视图控制器.在iPhone和iPod touch上,模态视图控制器始终以全屏显示,但在iPad上有几种不同的显示选项.

一旦视图控件显示自己,是否有办法确保导航栏在状态栏下方可见?我应该将文档解释为,你没有获得iPhone/iPod的任何选项,只能在iPad上?

以前,我使用的'UIViewController:presentModalViewController:animated'工作正常,但自iOS 5.0以来,API已被弃用,因此我转而使用新版本.

在视觉上,我要做的是从屏幕底部开始使用新控制器,就像过去使用的旧API一样.

[用代码更新]:

// 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];
Run Code Online (Sandbox Code Playgroud)

Man*_*uja 187

确实,如果您在iPhone上以模态方式呈现视图控制器,无论您如何将其呈现在导航控制器的顶视图控制器上或任何其他方式,它都将始终全屏显示.但您始终可以使用以下解决方法显示导航栏:

而不是呈现该视图控制器以模态方式呈现导航控制器,其根视图控制器设置为您想要的视图控制器:

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:^{

                        }];
Run Code Online (Sandbox Code Playgroud)

当您的视图以模态方式呈现时,您应该会看到一个导航栏.

  • 当我尝试呈现导航控制器时,它崩溃(''NSInvalidArgumentException',原因:'不支持推送导航控制器').这怎么办? (2认同)

Tal*_*ion 38

Swift 3.0/4.0

导航:

guard let myVC = self.storyboard?.instantiateViewController(withIdentifier: "MyViewController") else { return }
let navController = UINavigationController(rootViewController: myVC)

self.navigationController?.present(navController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

回去:

self.dismiss(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

Swift 2.0

导航:

let myVC = self.storyboard?.instantiateViewControllerWithIdentifier("MyViewController");
let navController = UINavigationController(rootViewController: myVC!)

self.navigationController?.presentViewController(navController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

回去:

self.dismissViewControllerAnimated(true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

  • 但是当我尝试你的代码时如何设置然后只设置导航栏,但我不能更改它的属性,如栏色调颜色、标题等。 (2认同)

小智 23

你能用:

[self.navigationController pushViewController:controller animated:YES];
Run Code Online (Sandbox Code Playgroud)

回去(我想):

[self.navigationController popToRootViewControllerAnimated:YES];
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,如果您的故事板中已经有导航控制器设计,那么最好的方法就是这样做.你帮助了我很多 (3认同)