Modal segue, navigation bar disappears
我使用Xcode4.6.1在Objective-C上进行编码。我想知道在两个视图控制器之间创建模式segue时如何保持导航栏的显示,因为我在故事板中执行segue,当我运行应用程序时,第二个视图控制器的导航栏将消失,我在该栏上有一个"完成"按钮,但我看不到T
只需将另一个
运行应用程序。现在它应该可以完美地工作了。
希望这能解决你的问题。
模态分段占据整个屏幕,因此呈现控制器中的任何导航栏、工具栏或选项卡栏都将被覆盖。如果您希望在这个模式控制器上有一个导航栏,您需要专门为它添加一个,并向新的导航栏(或工具栏)添加您想要的任何按钮。如果你不想这样做,那就不要以一种方式呈现它,去推动它。
您只需执行以下操作即可显示导航栏:
Objtovi-C
1 | UINavigationController alloc]initWithRootViewController:modalVC]; |
斯威夫特3
1 2 3 | let modelVC = self.storyboard?.instantiateViewController(withIdentifier:"modalVC") as! ModalVC let navBarOnModal: UINavigationController = UINavigationController(rootViewController: modalVC) self.present(navBarOnModal, animated: true, completion: nil) |
这将显示带有导航栏的模态视图控制器。关于客观C的知识是有限的,所以我没有写演讲的部分。你应该能搞清楚。;)
希望这有帮助!
在iOS 8中有一种更好的方法。您可以使用自适应表达样式:
ObjultC:
1 2 3 4 5 6 7 8 | - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller { return UIModalPresentationFullScreen; } - (UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style { UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: controller.presentedViewController]; return navController; } |
Swift:
1 2 3 4 5 6 7 8 9 | UIPopoverPresentationControllerDelegate func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle { return UIModalPresentationStyle.FullScreen } func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? { var navController = UINavigationController(rootViewController: controller.presentedViewController) return navController } |
斯威夫特4:
1 2 3 4 5 6 7 8 9 | extension MyViewController: UIPopoverPresentationControllerDelegate { func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle { return UIModalPresentationStyle.fullScreen } func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? { return UINavigationController(rootViewController: controller.presentedViewController) } } |
在准备Segue方法中,设置委托:
1 2 3 4 | override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { if let adpostVC = segue.destinationViewController as? XXXController { let popPC = adpostVC.popoverPresentationController popPC?.delegate = self |
SWIFT版:
下列步骤:
覆盖PrepareForSegue()。
1 2 3 4 5 6 7 8 9 10 | override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier =="goToYourController" { let navigation: UINavigationController = segue.destinationViewController as! UINavigationController var vc = YourViewController.init() vc = navigation.viewControllers[0] as! YourViewController //if you need send something to destnation View Controller //vc.delegate = self } } |
有一种更简单的方法可以做到这一点。如前所述(但没有解释所有步骤),您需要将所需的目标视图控制器嵌入到导航控制器中,然后将目标视图控制器设置为导航控制器,然后导航控制器调用目标视图控制器。
首先,将vc嵌入到navvc中。然后您必须编写代码,将SEgue目的地设置为NAV VC。
代码如下:
1 2 3 4 5 6 7 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { UINavigationController *nav = segue.destinationViewController; AddTriviaVC *triv = [[AddTriviaVC alloc]init]; triv = nav.viewControllers[0]; triv.location = self.location; } |
我希望这是有道理的。
在故事板中,您应该向新的ViewController添加导航项,然后为"完成"按钮添加条形按钮项。
这可能是因为您的模式中没有
这是我在Swift 4.2中的"Volodymyr Nazarkevych"的简短版本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | override func prepare(for segue: UIStoryboardSegue, sender: Any?) { super.prepare(for: segue, sender: sender) switch(segue.identifier ??"") { case"segueIdentifier": // name of your segue let navigation: UINavigationController = segue.destination as! UINavigationController //here you create new name of your destination ViewController and assign his name to let constant above guard let myNewViewController = navigation.viewControllers[0] as? DestinationViewController //DestinationViewController is your 2-ViewController where you go to from first one. else { fatalError("Unexpected destination: \(segue.destination)") } // TO DO SOMETHING HERE default: fatalError("Unexpected Segue Identifier; \(String(describing: segue.identifier))") } } |
"Volodymyr Nazarkevych"的代码工作得很好,但只有当您的segue是从1-viewController到2-viewController的导航控制器时,而不是直接到2-viewController。非常感谢Vova!.
同样,在目标块代码之后的切换情况下,您可以执行不同的操作,例如从第二个viewcontroller获取一些信息或文件。
您可以通过在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | NSInteger tbHeight = 50; tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - tbHeight), self.view.frame.size.width, tbHeight)]; tb.translucent = YES; emailButton = [[UIBarButtonItem alloc] initWithTitle:@"Email Results" style:UIBarButtonItemStyleBordered target:tvController action:@selector(actionSheet:)]; UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneButtonPressed:)]; NSArray *barButton = [[NSArray alloc] initWithObjects:emailButton,flexibleSpace,doneButton,nil]; [tb setItems:barButton]; [self.view addSubview:tb]; barButton = nil; |
然后,您必须创建一个IBaction来按"完成"按钮,操作如下:
1 2 3 | -(IBAction)doneButtonPressed:(id)sender { [self dismissModalViewControllerAnimated:YES]; } |
这应该会给您提供模态视图控制器想要的东西。