模态segue,导航栏消失

Joh*_*dah 48 objective-c ios xcode4.6

我正在使用Xcode 4.6.1在Objective-C上编码.我想知道当我在2个View控制器之间创建模态segue时如何保持导航栏显示,因为我在故事板中执行segue,当我运行应用程序时,第二个视图控制器的导航栏消失了,我在那个酒吧上有一个完成按钮,但我看不到它.

Sal*_*sum 83

只需Navigation Controller在模态视图控制器中添加另一个.按照步骤

  1. 选择 Modal View Controller
  2. Editor menu
  3. 选择 Embed In
  4. 选择 Navigation Controller

运行该应用程序.现在它应该完美地工作.

希望这能解决你的问题.

  • 虽然不喜欢有冗余屏幕(导航控制器)的想法只是为了显示一些顶部工具栏. (3认同)

rde*_*mar 58

模态segue占据整个屏幕,因此呈现控制器中的任何导航栏,工具栏或标签栏都将被掩盖.如果您想在此模态控制器上使用导航栏,则需要专门为其添加一个导航栏,并将所需的任何按钮添加到该新导航栏(或工具栏).如果你不想这样做,那么不要以模态方式呈现它,做一个推动它.


Chr*_*mit 17

您只需执行以下操作即可显示导航栏:

Objective-C的

UINavigationController alloc]initWithRootViewController:modalVC];
Run Code Online (Sandbox Code Playgroud)

SWIFT 3

let modelVC = self.storyboard?.instantiateViewController(withIdentifier: "modalVC") as! ModalVC
let navBarOnModal: UINavigationController = UINavigationController(rootViewController: modalVC)
self.present(navBarOnModal, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

这将显示带导航栏的模态视图控制器.关于Objective-C的知识是有限的,所以我没有写出演示的部分.你应该能够找到一个.;)

希望这可以帮助!


小智 10

在iOS 8中有一种更好的方法.您可以使用自适应演示样式:

  1. 使用"作为弹出窗口"segue
  2. 将控制器设置为采用UIPopoverPresentationControllerDelegate协议
  3. 实施2种方法

Objective-C的:

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationFullScreen;
}

- (UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style {
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: controller.presentedViewController];
    return navController;
}
Run Code Online (Sandbox Code Playgroud)

迅速:

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
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特4:

extension MyViewController: UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.fullScreen
    }

    func presentationController(_ controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
        return UINavigationController(rootViewController: controller.presentedViewController)
    }
}
Run Code Online (Sandbox Code Playgroud)

在准备segue方法时设置委托:

   override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if let adpostVC = segue.destinationViewController as? XXXController {
            let popPC = adpostVC.popoverPresentationController
            popPC?.delegate = self
Run Code Online (Sandbox Code Playgroud)


小智 5

swift版本:

按照步骤:

  1. 在NavigationController中嵌入VC
  2. 覆盖prepareForSegue()

    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
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)