Disable/enable scrolling in UIPageViewController

后端 未结 5 920
轮回少年
轮回少年 2020-12-31 14:50

I got a viewController which inherits from UIPageViewController ( @interface PageScrollViewController : UIPageViewController ) Now I\'

相关标签:
5条回答
  • 2020-12-31 15:03

    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;
    
    0 讨论(0)
  • 2020-12-31 15:04

    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
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-31 15:05

    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
    
    0 讨论(0)
  • 2020-12-31 15:07

    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
    }
    
    0 讨论(0)
  • 2020-12-31 15:13

    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.

    0 讨论(0)
提交回复
热议问题