简体   繁体   中英

how to dismiss a modal presentation of UISplitController in Portrait orientation

my project is in swift 2 and works in both orientation.

i have a UISplitViewController presented modally.

in the navigation bar of master view i have a bar button for dismiss this.

when i open the controller in landscape mode the close button work well because master view is always visibile and his parent and presenting view controller are ok.

the problem is in the portrait orientation because master view isn't always visible and his parent and presenting controller are null when i tap the cancel button.

there is a solution?

thanks in advance

presenting controller

i present a modal splitViewController from storyboard:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if segue.identifier == "splitSegue" {

    let split = segue.destinationViewController as! UISplitViewController
    let masterNav = split.viewControllers[0] as! UINavigationController
    let master = masterNav.topViewController as! MasterController
    master.delegate = self
}}

and this is the delegate fun for dismiss:

func updateAppCategory(master: MasterController) {

    self.presentedViewController?.dismissViewControllerAnimated(true, completion: nil)
}

I had have the same issue and found a solution:

splitViewController!.preferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible

When you insert this in viewdidload, then you can dismiss the splitviewcontroller in portrait and landscape mode

The apple recommended way of dismissing the presented view controller is, letting the presenting view controller handle the dismissal. The view controller that's presented shouldn't dismiss itself.

Use a delegate approach, make a didPressCancel method in the delegate, and call it when user press cancel. And from presenting view controller, in the implementation of this method, dismiss the presented view controller.

Edgar's solution works however if you don't want to use that display mode you can just set PreferredDisplayMode.AllVisible immediately prior to dismissing the Split View (instead of in ViewDidLoad )

This is the Xamarin.iOS code I used in my master controller. Works in portrait and landscape.

partial void btnExitModalSplitViewControllerAllOrientations_Activated(UIBarButtonItem sender)
{
    SplitViewController.PreferredDisplayMode = UISplitViewControllerDisplayMode.AllVisible;
    SplitViewController.DismissViewController(true, null);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM