So I have a UIPageViewController where I want to implement the spineLocation to be .mid if the device is in landscape and .min for portrait, therefore showing two controllers in landscape and one in portrait.
In the page view controller delegate I implemented this function:
func pageViewController(_ pageViewController: UIPageViewController, spineLocationFor orientation: UIInterfaceOrientation) -> UIPageViewController.SpineLocation {
if orientation == .landscapeLeft || orientation == .landscapeRight {
return .mid
} else {
return .min
}
}
In my UIPageViewController class, to set the view controllers I have this function:
func setControllers() {
var controllers: [UIViewController] = [controller1]
if spineLocation == .mid {
controllers.append(controller2)
}
setViewControllers(controllers, direction: .forward, animated: true, completion: nil)
}
My problem is that I am not sure of where to put it, I tried in:
- viewDidLoad (before setting pageController delegate)
- viewDidLoad (after setting pageController delegate)
- viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
- didRotate(from fromInterfaceOrientation: UIInterfaceOrientation)
- willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval)
The view is shown correctly in portrait mode but when I start or turn the device to landscape the app crashes for:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The number of provided view controllers (1) doesn't match the number required (2) for the requested spine location (UIPageViewControllerSpineLocationMid)'