Hiding the master view controller with UISplitViewController in iOS8

后端 未结 10 969
囚心锁ツ
囚心锁ツ 2020-12-12 19:06

I have an iOS7 application, which was based on the Xcode master-detail template, that I am porting to iOS8. One area that has changed a lot is the UISplitViewControlle

相关标签:
10条回答
  • 2020-12-12 19:29

    Use preferredDisplayMode. In didSelectRowAtIndexPath or prepareForSegue:

    self.splitViewController?.preferredDisplayMode = .PrimaryHidden
    self.splitViewController?.preferredDisplayMode = .Automatic
    

    Unfortunately the master view abruptly disappears instead of sliding away, despite the documentation stating:

    If changing the value of this property leads to an actual change in the current display mode, the split view controller animates the resulting change.

    Hopefully there is a better way to do this that actually animates the change.

    0 讨论(0)
  • 2020-12-12 19:29

    for iPad add Menu button like this

    UIBarButtonItem *menuButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"burger_menu"]
                                                                           style:UIBarButtonItemStylePlain
                                                                          target:self.splitViewController.displayModeButtonItem.target
                                                                          action:self.splitViewController.displayModeButtonItem.action];
    [self.navigationItem setLeftBarButtonItem:menuButtonItem];
    

    This work great with both landscape and portrait mode. To programmatically close the popover vc you just need to force the button action like this

    [self.splitViewController.displayModeButtonItem.target performSelector:appDelegate.splitViewController.displayModeButtonItem.action];
    
    0 讨论(0)
  • 2020-12-12 19:30

    Very similar to the method by phatmann, but a bit simpler in Swift 5. And it's not technically a 'hack', as it is what the iOS doc suggested.

    In your prepareForSegue or other methods that handle touches, in

    let barButton = self.splitViewController?.displayModeButtonItem
    _ = barButton?.target?.perform(barButton?.action)
    

    According to Apple, the splitViewController's displayModeButtonItem is set up for you to display the master view controller in a way that suits your device orientation. That is, .preferHidden in portrait mode.

    All there's to do is to press the button, programatically. Or you can put it in an extension to UISplitViewController, like phatmann did.

    0 讨论(0)
  • 2020-12-12 19:32

    try

    let svc = self.splitViewController
    svc.preferredDisplayMode = UISplitViewControllerDisplayMode.PrimaryHidden
    0 讨论(0)
  • 2020-12-12 19:34

    The code below hides the master view with animation

    UIView.animateWithDuration(0.5) { () -> Void in
                self.splitViewController?.preferredDisplayMode = .PrimaryHidden
            }
    
    0 讨论(0)
  • 2020-12-12 19:44

    My solution in the Swift 1.2

      override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
        var screen = UIScreen.mainScreen().currentMode?.size.height
        if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad) || screen >= 2000 && UIDevice.currentDevice().orientation.isLandscape == true  && (UIDevice.currentDevice().userInterfaceIdiom == .Phone){
            performSegueWithIdentifier("showDetailParse", sender: nil)
            self.splitViewController?.preferredDisplayMode = UISplitViewControllerDisplayMode.PrimaryHidden
        } else if (UIDevice.currentDevice().userInterfaceIdiom == .Phone) {
            performSegueWithIdentifier("showParse", sender: nil)
        }
    }
    
    0 讨论(0)
提交回复
热议问题