Search code examples
iosiphoneswiftuipageviewcontroller

How to set UIPageViewControllerSpineLocation to .mid when in using transitionStyle .scroll


I am in the process of creating an iPhone flashcard app that will be used to study various terms. Currently, I am using a UIPageViewController to display each consecutive card (view controller). When the user taps a card it will flip and show them the back, and when they swipe left or right, they can scroll between cards. Current App interface

So far this works great, but I want to be able to show both the front and the back sides of the card when in landscape orientation. Normally this would be easy to do by using the UIPageViewControllerDelegate's method func pageViewController(_ pageViewController: UIPageViewController, spineLocationFor orientation: UIInterfaceOrientation) and returning .mid if the orientation is horizontal. However, since I am not using .pageCurl but .scroll as my pageViewController's transitionStyle, this method is never called. Apple Documentation It also cannot be set manually when not using .pageCurl. Apple's docs say:

A spine location is only valid if the transition style is pageCurl. If the transition style is pageCurl, the default value for this property is min; otherwise, the default is none.

I've tried to add a method that is called when the orientation is landscape:

func showBothSides(){
    let currentViewController = self.pageViewController!.viewControllers![0] as! PracticePageDataVC
    let viewControllers: [UIViewController] = [currentViewController, currentViewController]
    self.pageViewController!.setViewControllers(viewControllers, direction: .forward, animated: true, completion: {done in })
}

But this returns the error: "'The number of view controllers provided (2) doesn't match the number required (1) for the requested transition'"

So basically I am looking for an alternative solution to show two view controllers side by side while still maintaining the functionality of the horizontal scroll transition between cards.


Solution

  • So basically I am looking for an alternative solution to show two view controllers side by side while still maintained the functionality of scrolling horizontally between cards

    You have two choices.

    • Your "page" in the UIPageViewController can itself be a custom parent (container) view controller whose two children's views are shown side by side.

    • You can drop the use of UIPageViewController completely, and implement your own interface using a horizontal paging scroll view (that's what we did before page view controllers existed, after all).