How do you know what is the current page/view displayed inside an UIPageViewController
?
I have overridden the viewDidAppear
method of my child views, so that they send an id to the parent view in their viewDidAppear
method.
However, the problem is this: i cannot reliably use that id as id for the displayed page. because if the user turns the page but halfway through decides to stop the turning and put the page back, viewDidAppear
will already have been called. (the view is visible behind the curled page).
Maybe i should only switch to a new id if the current view disappears. But I wonder if there is not a more simple way to return the view that is currently visible?
Unfortunately, all above methods didn't help me. Nevertheless, I have found the solution by using tags. May be it's not the best, but it works and hope it helps someone:
In Swift: (thanks to @Jessy)
Example: gist
As of iOS 6 I've found that the
viewControllers
property of UIPageViewController constantly updates so that it will always hold the one view controller that represents the current page, and nothing else. Thus, you can access the current page by callingviewControllers[0]
(Assuming you only show one view controller at a time).The viewController array only updates once the page "locks" into place, so if a user decides to partially reveal the next page it doesn't become the "current" page unless they complete the transition.
If you want to keep track of the "page numbers" assign your view controllers an index value as you create them through the UIPageViewController datasource methods.
So for example:
But note the comments that this is unreliable.
Building on Ole's Answer…
This is how I implemented the 4 methods to track the current page and update the page indicator to the correct index:
This works for me reliably
I have a custom UIPageController. This pageController.currentPage is updated from the displayed UIViewController in the viewWillAppear
Swift 4
No unnecessary code. 3 ways of doing it. Using UIPageViewControllerDelegate method.