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?
I have a viewControllers array, that I display in the UIPageViewController.
Important thing to note here is that the setViewControllers method of UIPageViewController does not give any delegate callback. The delegate callbacks only represent user touch actions in the UIPageViewController.
I am keeping track of the page index by using a small function and specifying pageIndex as static NSInteger.
and calling
[self setPageIndex];
inside the function specified by Ole and also after detecting the change in orientation.The solution below worked for me.
Apple could avoid a lot of hassle by making the native UIPageViewController scroll view pagination more configurable. I had to resort to overlaying a new UIView and UIPageControl just because the native UIPageViewController pagination won't support a transparent background or repositioning within the view frame.
How about asking for a
viewController
directly from theUIPageViewController
(Swift 4 version):Or, if you just need the currently presented view controller at some point of time, simply use
pageViewController.viewControllers?.first
at that time.The simplest way to approach this IMHO is to use the PageControl to store the potential outcome of the transition and then revert if the transition was cancelled. This means that the page control changes as soon as the user starts swiping, which is ok by me. This requires that you have your own array of UIViewControllers (in this example called
allViewControllers
)Keeping track of our current page number in the delegate methods:
We use a helper getVC to update our page number and return the next/prev vc. This is a good place to do this because getVC only gets called if there is a next or previous vc.