In my case parent UIViewController
contains UIPageViewController
which contains UINavigationController
which contains UIViewController
. I want to add a swipe gesture to the last view controller, but swipes are handled as if they belong to page view controller. I tried to do this both programmatically and via xib but with no result.
So as I understand I can't achieve my goal until UIPageViewController
handles its gestures. How to solve this issue?
The documented way to prevent the
UIPageViewController
from scrolling is to not assign thedataSource
property. If you assign the data source it will move into 'gesture-based' navigation mode which is what you're trying to prevent.Without a data source you manually provide view controllers when you want to with
setViewControllers:direction:animated:completion
method and it will move between view controllers on demand.The above can be deduced from Apple's documentation of UIPageViewController (Overview, second paragraph):
If you want your
UIPageViewController
to maintain it's ability to swipe, while allowing your content controls to use their features (Swipe to delete, etc), just turn offcanCancelContentTouches
in theUIPageViewController
.Put this in your
UIPageViewController
'sviewDidLoad
func. (Swift)The
UIPageViewController
has an auto-generated subview that handles the gestures. We can prevent these subviews from cancelling content gestures.From...
Swipe to delete on a tableView that is inside a pageViewController
Similar to @user3568340 answer
Swift 4
Edit: this answer works for page curl style only. Jessedc's answer is far better: works regardless of the style and relies on documented behavior.
UIPageViewController
exposes its array of gesture recognizers, which you could use to disable them:(Swift 4) You can remove gestureRecognizers of your pageViewController:
If you prefer in extension:
and
pageViewController.removeGestureRecognizers
I've been fighting this for a while now and thought I should post my solution, following on from Jessedc's answer; removing the PageViewController's datasource.
I added this to my PgeViewController class (linked to my page view controller in the storyboard, inherits both
UIPageViewController
andUIPageViewControllerDataSource
):This can then be called when each sub view appears (in this case to disable it);
I hope this helps someone out, its not as clean as I would like it but doesn't feel too hacky etc.
EDIT: If someone wants to translate this into Objective-C please do :)