presentViewController并显示导航栏

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

'UIViewController:presentViewController:viewControllerToPresent:animated:completion' 

“presentViewController:animated:completion:”的文档注意:

“在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]; 

确实,如果您在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:^{ }]; 

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

你可以使用:

 [self.navigationController pushViewController:controller animated:YES]; 

回去(我认为):

 [self.navigationController popToRootViewControllerAnimated:YES]; 

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) 

回去:

 self.dismiss(animated: true, completion: nil) 

Swift 2.0

导航:

 let myVC = self.storyboard?.instantiateViewControllerWithIdentifier("MyViewController"); let navController = UINavigationController(rootViewController: myVC!) self.navigationController?.presentViewController(navController, animated: true, completion: nil) 

回去:

 self.dismissViewControllerAnimated(true, completion: nil) 

所有[self.navigationController pushViewController:controller animated:YES]; 确实是一个过渡animation,并将其添加到导航控制器堆栈和其他一些很酷的导航栏animation的东西。 如果你不关心酒吧animation,那么这个代码应该工作。 酒吧确实出现在新的控制器,你得到一个互动的stream行手势!

 //Make Controller DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]]; //Customize presentation controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical; controller.modalPresentationStyle = UIModalPresentationCurrentContext; //Present controller [self presentViewController:controller animated:YES completion:nil]; //Add to navigation Controller [self navigationController].viewControllers = [[self navigationController].viewControllers arrayByAddingObject:controller]; //You can't just [[self navigationController].viewControllers addObject:controller] because viewControllers are for some reason not a mutable array. 

编辑:对不起,presentViewController将填满整个屏幕。 您将需要进行自定义转换,使用CGAffineTransform.translation或其他方法,使用转换对控制器进行animation处理,然后将其添加到navigationController的viewControllers。

我对ios7有同样的问题。 我在select器中调用它,它同时在ios7和ios8上工作。

 [self performSelector: @selector(showMainView) withObject: nil afterDelay: 0.0]; - (void) showMainView { HomeViewController * homeview = [ [HomeViewController alloc] initWithNibName: @ "HomeViewController" bundle: nil]; UINavigationController * navcont = [ [UINavigationController alloc] initWithRootViewController: homeview]; navcont.navigationBar.tintColor = [UIColor whiteColor]; navcont.navigationBar.barTintColor = App_Theme_Color; [navcont.navigationBar setTitleTextAttributes: @ { NSForegroundColorAttributeName: [UIColor whiteColor] }]; navcont.modalPresentationStyle = UIModalPresentationFullScreen; navcont.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self.navigationController presentViewController: navcont animated: YES completion: ^ { }]; } 

如果您没有设置modalPresentationStyle属性(如UIModalPresentationFormSheet),则导航栏将始终显示。 为了确保,总是这样做

 [[self.navigationController topViewController] presentViewController:vieController animated:YES completion:nil]; 

这将始终显示导航栏。

我使用这个代码在ios 8中工作正常。

  MyProfileEditViewController *myprofileEdit=[self.storyboard instantiateViewControllerWithIdentifier:@"myprofileeditSid"]; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myprofileEdit]; [self presentViewController:navigationController animated:YES completion:^{ }]; 

如果您在Swift 2.x中使用NavigationController

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let targetViewController = storyboard.instantiateViewControllerWithIdentifier("targetViewControllerID") as? TargetViewController self.navigationController?.pushViewController(targetViewController!, animated: true) 

尝试这个

  let transition: CATransition = CATransition() let timeFunc : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) transition.duration = 1 transition.timingFunction = timeFunc transition.type = kCATransitionPush transition.subtype = kCATransitionFromRight self.view.window!.layer.addAnimation(transition, forKey: kCATransition) self.presentViewController(vc, animated:true, completion:nil) 

Swift 3

  let vc0 : ViewController1 = ViewController1() let vc2: NavigationController1 = NavigationController1(rootViewController: vc0) self.present(vc2, animated: true, completion: nil) 

Swift版本:这提出了一个embedded在导航控制器中的ViewController。

  override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) // Identify the bundle by means of a class in that bundle. let storyboard = UIStoryboard(name: "Storyboard", bundle: NSBundle(forClass: SettingsViewController.self)) // Instance of ViewController that is in the storyboard. let settingViewController = storyboard.instantiateViewControllerWithIdentifier("SettingsVC") let navController = UINavigationController(rootViewController: settingViewController) presentViewController(navController, animated: true, completion: nil) } 

一个解决scheme

 DetailViewController *controller = [[DetailViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller]; navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; navController.modalPresentationStyle = UIModalPresentationCurrentContext; [self.navigationController presentViewController:navController animated:YES completion:nil];