2
votes

I'm making my first universal app, so far so good, but I have a problem with UISplitViewController on iPad.

how can i make the UISplitViewController act same as on iPhone when it is in portrait mode?

like in portrait mode show only master screen when i click on it, it navigate to the details screen, and when in landscape mode show both of them beside each other.

what happens now , is that it shows the details screen only in portrait and show both of them in landscape mode.

for iPhone i used this code in master view to solve this issue

func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController: UIViewController, ontoPrimaryViewController primaryViewController: UIViewController) -> Bool {

    return true
}

but this didn't work with iPad, i found another code on here but didn't work too.

 func splitViewController(svc: UISplitViewController, willHideViewController aViewController: UIViewController, withBarButtonItem barButtonItem: UIBarButtonItem, forPopoverController pc: UIPopoverController) {

    self.navigationItem.leftBarButtonItem?.target?.performSelector((self.navigationItem.leftBarButtonItem?.action)!, withObject: self.navigationItem)
}

other code maybe you need to know, i added those in viewDidLoad in master view controller

    self.splitViewController?.delegate = self
    self.splitViewController?.preferredDisplayMode = UISplitViewControllerDisplayMode.PrimaryOverlay

    self.splitViewController!.maximumPrimaryColumnWidth = splitViewController!.view.bounds.size.width;
    self.splitViewController!.preferredPrimaryColumnWidthFraction = 0.3

so please if anyone can help me find solution for this issue, I will be very thankful

1

1 Answers

1
votes

UISplitViewController use size classes to determine how to display his master and detail view controller.

When your UISplitViewController has horizontalSizeClass and verticalSizeClass Regular it will display both the Master and Detail view controllers on the same screen.

You'll need to embed your split view controller into a container view controller to override the default size class as I explained here.

You also have to check the device orientation to fork between Compact (when Portrait) or Regular (when Landscape) horizontal size class:

class ContainerVC: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    performOverrideTraitCollection()
}

private func performOverrideTraitCollection() {
    let orientation = UIDevice.currentDevice().orientation
    var isPortrait = false
    switch orientation {
    case .Portrait, .PortraitUpsideDown:
        isPortrait = true
    default:
        isPortrait = false
    }
    for childVC in self.childViewControllers {
        self.traitCollection.userInterfaceIdiom
        setOverrideTraitCollection(UITraitCollection(horizontalSizeClass: isPortrait ? .Compact : .Regular), forChildViewController: childVC)
    }
}
}