我有一个视图控制器层次结构,最顶层的控制器显示为模态,并想知道如何在使用时显示导航栏
'UIViewController:presentViewController:viewControllerToPresent:animated:completion'
'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];
答案 0 :(得分:180)
确实,如果您在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:^{
}];
当您的视图以模态方式显示时,您应该会看到一个导航栏。
答案 1 :(得分:33)
Swift 3.0 / 4.2
<强>导航
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)
答案 2 :(得分:23)
你能用:
[self.navigationController pushViewController:controller animated:YES];
回去(我想):
[self.navigationController popToRootViewControllerAnimated:YES];
答案 3 :(得分:2)
我在ios7上遇到了同样的问题。我在选择器中调用它,它在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: ^ {
}];
}
答案 4 :(得分:1)
所有[self.navigationController pushViewController:controller animated:YES];
都会为转换设置动画,并将其添加到导航控制器堆栈以及其他一些很酷的导航栏动画内容。如果你不关心条形动画,那么这段代码应该工作。条形图确实出现在新控制器上,您将获得一个交互式弹出手势!
//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或其他东西进行自定义转换,使用转换为控制器设置动画,然后将其添加到navigationController的viewControllers中。
答案 5 :(得分:1)
Swift 3
let vc0 : ViewController1 = ViewController1()
let vc2: NavigationController1 = NavigationController1(rootViewController: vc0)
self.present(vc2, animated: true, completion: nil)
答案 6 :(得分:0)
如果未设置modalPresentationStyle属性(如UIModalPresentationFormSheet),则将始终显示导航栏。为了确保,总是这样做
[[self.navigationController topViewController] presentViewController:vieController
animated:YES
completion:nil];
这将始终显示导航栏。
答案 7 :(得分:0)
一种解决方案
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];
答案 8 :(得分:0)
我使用此代码。它在iOS 8中运行良好。
MyProfileEditViewController *myprofileEdit=[self.storyboard instantiateViewControllerWithIdentifier:@"myprofileeditSid"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myprofileEdit];
[self presentViewController:navigationController animated:YES completion:^{}];
答案 9 :(得分:0)
如果您在Swift 2.x中使用NavigationController
rename
答案 10 :(得分:0)
试试这个
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)
答案 11 :(得分:0)
Swift版本: 这提供了嵌入在导航控制器中的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)
}