How do I Disable the swipe gesture of UIPageViewCo

2019-01-04 07:58发布

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?

13条回答
Deceive 欺骗
2楼-- · 2019-01-04 08:14

A useful extension of UIPageViewController to enable and disable swipe.

extension UIPageViewController {

    func enableSwipeGesture() {
        for view in self.view.subviews {
            if let subView = view as? UIScrollView {
                subView.isScrollEnabled = true
            }
        }
    }

    func disableSwipeGesture() {
        for view in self.view.subviews {
            if let subView = view as? UIScrollView {
                subView.isScrollEnabled = false
            }
        }
    }
}
查看更多
虎瘦雄心在
3楼-- · 2019-01-04 08:14

I solved it like this (Swift 4.1)

if let scrollView = self.view.subviews.filter({$0.isKind(of: UIScrollView.self)}).first as? UIScrollView {
             scrollView.isScrollEnabled = false
}
查看更多
地球回转人心会变
4楼-- · 2019-01-04 08:15
for (UIScrollView *view in self.pageViewController.view.subviews) {

    if ([view isKindOfClass:[UIScrollView class]]) {

        view.scrollEnabled = NO;
    }
}
查看更多
劫难
5楼-- · 2019-01-04 08:16

Thanks to @user2159978's answer.

I make it a little more understandable.

- (void)disableScroll{
    for (UIView *view in self.pageViewController.view.subviews) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            UIScrollView * aView = (UIScrollView *)view;
            aView.scrollEnabled = NO;
        }
    }
}
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-04 08:20

Implementing @lee's (@user2159978's) solution as an extension:

extension UIPageViewController {
    var isPagingEnabled: Bool {
        get {
            var isEnabled: Bool = true
            for view in view.subviews {
                if let subView = view as? UIScrollView {
                    isEnabled = subView.isScrollEnabled
                }
            }
            return isEnabled
        }
        set {
            for view in view.subviews {
                if let subView = view as? UIScrollView {
                    subView.isScrollEnabled = newValue
                }
            }
        }
    }
}

Usage: (in UIPageViewController)

self.isPagingEnabled = false
查看更多
一纸荒年 Trace。
7楼-- · 2019-01-04 08:21

I translate answer of user2159978 to Swift

func removeSwipeGesture(){
    for view in self.pageViewController!.view.subviews {
        if let subView = view as? UIScrollView {
            subView.scrollEnabled = false
        }
    }
}
查看更多
登录 后发表回答