I have a UIPageViewController, which works fine when we swipe left or right to turn pages.
class ViewController: UIViewController, UIPageViewControllerDataSource {
...
}
Now I intend to provide Previous/Next button on the page too, so that pages can be turned by clicking on these buttons.
How can I trigger the swipe left/right behaviour OR turn pages programmatically?
Note
This is a question for Swift language, not Objective-C.
Just leaving this out here incase any one wants to use a defined protocol to navigate around and change view controllers programatically.
Use the above protocol and confirm to root UIPageViewController to manage navigation between view controllers.
Example below:
Once achieved all that, child view controllers that need to make UIPageViewController move to next or previous page can use AppWalkThroughDelegate methods by passing a specific number onto delegate property.
Example below: Delegate method invoked once button pressed
There's generally no need to fiddle around with page indexes and what not. Chances are you are already implementing a
dataSource: UIPageViewControllerDataSource
, which has all of what you need to get a previous or next ViewController to display.Swift 2.2 Example:
This way you're guaranteed that the pages you're transitioning to are exactly what the PageViewController's built in gestures would trigger.
Use this funtion and set the transition style for the animation you want.
Here is a swift implementation of this:
viewControllerAtIndex(index: Int) -> UIViewController?
is my own function to get the correct view controller to swipe to.The Swift 3 version of SuitedSloth's answer (with a small tweak to the
animated
parameter as I needed it to be animated by default, but still taking a parameter in the function) in case anyone needs it:Here is the swift 4 implementation with the delegates as well.
Since I also use the UIPageViewControllerDelegate, and the delegate methods weren't called with most of the setViewController solutions.
Thanks to Andrew Duncan's for his comment about the delegate.