Search code examples
iosswiftuipageviewcontroller

UIPageViewController - start not at the beginning, but in the middle (page 2)


I have set up a PageViewController with three pages. However, I want it to start at page 2 (in the middle). But I don't know how to do this.

I know you set the start viewController like this:

pageViewController.setViewControllers([viewControllerAtIndex(startIndex)!], direction: .forward, animated: true, completion: nil)

But you can only say direction forward or reverse. But in this case, it's both.

What can I do? Thank you!


Solution

  • Your implementation for the startViewController is correct

    pageViewController.setViewControllers([viewControllerAtIndex(startIndex)!], direction: .forward, animated: false, completion: nil)
    

    In your UIPageViewControllerDataSource implementation, also implement this method:

    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return startIndex
    }
    

    You might also need

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return pageData.count
    }