我有一个视图控制器层次结构,最顶层的控制器显示为模式,想知道使用时如何显示导航栏
'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:^{
}];
当您的视图以模态方式呈现时,您应该会看到一个导航栏。
斯威夫特 5.*
导航:
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)
斯威夫特 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];
回去(我认为):
[self.navigationController popToRootViewControllerAnimated:YES];
我在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: ^ {
}];
}
[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。
斯威夫特 3
let vc0 : ViewController1 = ViewController1()
let vc2: NavigationController1 = NavigationController1(rootViewController: vc0)
self.present(vc2, animated: true, completion: nil)
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)
}
我使用此代码。它在 iOS 8 中运行良好。
MyProfileEditViewController *myprofileEdit=[self.storyboard instantiateViewControllerWithIdentifier:@"myprofileeditSid"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myprofileEdit];
[self presentViewController:navigationController animated:YES completion:^{}];
一种解决方案
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];
如果您没有设置 modalPresentationStyle 属性(如 UIModalPresentationFormSheet),导航栏将始终显示。为确保,始终做到
[[self.navigationController topViewController] presentViewController:vieController
animated:YES
completion:nil];
这将始终显示导航栏。
如果你在 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)
(void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
'NSInvalidArgumentException', reason: 'Pushing a navigation controller is not supported'
)。这怎么行?