9

I got a viewController which inherits from UIPageViewController ( @interface PageScrollViewController : UIPageViewController ) Now I'm wondering how I can enable and disable the scrolling from the UIPageViewController? When using a UIScrollView you would do setScrollEnabled:NO and self.view.userInteractionEnabled = NO; isn't an option since this blocks the whole UIView instead of just the scrolling.

EDIT This is in the PageScrollViewController : UIPageViewController class:

if ([[notification name] isEqualToString:@"NotificationDisable"]){
    NSLog (@"Successfully received the disable notification!");
    for (UIGestureRecognizer *recognizer in self.gestureRecognizers) {
        recognizer.enabled = NO;
    }
}
Shinonuma
  • 491
  • 2
  • 8
  • 19

5 Answers5

12

Or you can cast in your PagingVC To disable paging:

self.delegate = nil;
self.dataSource = nil;

And for enable it again:

self.delegate = self;
self.dataSource = self;
Mr.Fingers
  • 1,105
  • 1
  • 11
  • 15
10

Try looping through the gestureRecognizers of the UIPageViewController and disable/enable them:

for (UIGestureRecognizer *recognizer in pageViewController.gestureRecognizers) {        
        recognizer.enabled = NO;
}

Note: as found in this SO post, this method will only work for UIPageViewControllerTransitionStylePageCurl. You may want to try this solution (although it seems to be a bit hacky).

 for recognizer in pageViewController.gestureRecognizers {
    recognizer.isEnabled = false
}
tilo
  • 14,009
  • 6
  • 68
  • 85
  • 1
    Where do you execute this loop? Would be helpful to see some code. – tilo Apr 05 '13 at 08:57
  • 1
    So you're getting the "Successfully received ..." message? If so, try `NSLog(@"Gesture recognizers: %@",self.gestureRecognizers)` – tilo Apr 05 '13 at 09:12
  • Yes I get the "Successfully received..." messsage. The NSLog shows me: Gesture recognizers: ( ) – Shinonuma Apr 05 '13 at 09:48
  • With the second solution you gave me, how can I disable and enable the scroll then if I've put the code in viewDidLoad? – Shinonuma Apr 05 '13 at 10:03
  • 5
    It is already stated in this answer: `One workaround that comes to my mind is laying a transparent subview on top of your UIPageViewController and add to it a UIPanGestureRecognizer to intercept that kind of gesture and not forward further. You could enable this view/recognizer when disabling the gesture is required.` – tilo Apr 05 '13 at 10:05
8

I did the following thing (I have a controller that holds UIPageViewController).

self.pageController.view.userInteractionEnabled = NO;

And when you want to enable swipe or scroll, just enable user interaction.

Nemanja
  • 455
  • 4
  • 15
  • 3
    Unfortunately this also disables any controls you have in the vc. – Fook Dec 03 '15 at 22:11
  • 1
    As @Shinonuma said: "self.view.userInteractionEnabled = NO; isn't an option since this blocks the whole UIView instead of just the scrolling." – Mário Carvalho Jan 12 '16 at 19:23
  • 1
    @MárioCarvalho yes, it disables any gesture, that is the flaw of this solution. :( I posted this, since it can help to someone and be enough (like for me) while searching its solution. – Nemanja Jan 13 '16 at 11:04
3

Setting the UIPageViewController dataSource property to nil prevents scrolling, because the page view controller does not have a way to determine the "next" view controller to transition to.

self.dataSource = nil // scrolling disabled

self.dataSource = self // scrolling enabled
Frederic Adda
  • 5,905
  • 4
  • 56
  • 71
  • 1
    this is the best answer actually, in case your tab page is also a scrollview - this disables paging but gives you control over the vc still – Jacky Wang Feb 23 '20 at 22:22
3

UIPageViewController manages a UIScrollView internally to get things done. We can find out that UIScrollView and update its isScrollEnabled property.

let view = myPageViewController.view
for subview in view.subviews {
    if let scrollview = subview as? UIScrollView {
        scrollview.isScrollEnabled = false
        break
    }
}

Or use this UIPageViewController extension.

extension UIPageViewController {

    var scrollView: UIScrollView {
        for subview in view.subviews {
            if let scrollview = subview as? UIScrollView {
                return scrollview
            }
        }
        fatalError()
    }
    
    var isScrollEnabled: Bool {
        get {
            return scrollView.isScrollEnabled
        }
        set {
            scrollView.isScrollEnabled = newValue
        }
    }
}
Rui L
  • 251
  • 2
  • 7