Modal segue, navigation bar disappears

前端 未结 11 1217
既然无缘
既然无缘 2020-12-01 04:30

I\'m using Xcode 4.6.1 to code on Objective-C. I want to know how can I keep the navigation bar shown when I create a modal segue between 2 View controllers, because I\'m do

11条回答
  •  青春惊慌失措
    2020-12-01 04:46

    In iOS 8 there is a better method. You can use adaptive presentation styles:

    1. Use a "Present as popover" segue
    2. Set your controller to adopt the UIPopoverPresentationControllerDelegate protocol
    3. Implement 2 methods

    Objective-C:

    - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
        return UIModalPresentationFullScreen;
    }
    
    - (UIViewController *)presentationController:(UIPresentationController *)controller viewControllerForAdaptivePresentationStyle:(UIModalPresentationStyle)style {
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: controller.presentedViewController];
        return navController;
    }
    

    Swift:

    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
    }
    

    Swift 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)
        }
    }
    

    In prepare for segue method set the delegate:

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

提交回复
热议问题