Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable/enable scrolling in UIPageViewController

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;
    }
}
like image 557
Shinonuma Avatar asked Apr 05 '13 08:04

Shinonuma


4 Answers

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
        }
    }
}
like image 61
LuRui Avatar answered Oct 22 '22 15:10

LuRui


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;
like image 12
Mr.Fingers Avatar answered Oct 23 '22 21:10

Mr.Fingers


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
}
like image 10
tilo Avatar answered Oct 23 '22 19:10

tilo


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.

like image 8
Nemanja Avatar answered Oct 23 '22 21:10

Nemanja