6

In my project i use UIPageViewController to swipe between 5 child UIViewController. In some of child view controller, i need to disable the swipe gesture of the UIPageViewController so when user swipe it not change to other view. So how i can disable the swipe from the child view controller?

Appreciate for help..thanks

Voyager
  • 399
  • 1
  • 8
  • 15

2 Answers2

24

In your page view controller, add following

override func viewDidLoad(){
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourpageviewcontroller.enableSwipe(_:)), name:"enableSwipe", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(yourpageviewcontroller.disableSwipe(_:)), name:"disableSwipe", object: nil)

}
func disableSwipe(notification: NSNotification){
    self.dataSource = nil
}

func enableSwipe(notification: NSNotification){
    self.dataSource = self
}

In your child view controller, you can just post notification by following.

NSNotificationCenter.defaultCenter().postNotificationName("enableSwipe", object: nil)

OR

NSNotificationCenter.defaultCenter().postNotificationName("disableSwipe", object: nil)
Rurouni
  • 963
  • 10
  • 31
  • Hi, its working for swipe but it also block the scroll up and down since my child has uitableview. How to make the tableview keep scroll also?thanks – Voyager Aug 02 '16 at 03:42
  • Please check your child view's 'User Interaction Enabled' and also In your tableView, make sure Scrolling Enabled check box is checked – Rurouni Aug 02 '16 at 03:47
  • it turns out that my logic for checking the offset its wrong. so i accepted the answer. Thanks! – Voyager Aug 02 '16 at 04:06
  • Thank you very much, such a clean answer, +1 :) – mAc Aug 29 '17 at 21:30
  • 1
    This is really what I needed! I didnt know you could do that kind of things with Notification Center! thanks man – arvidurs Sep 29 '17 at 06:32
6

You can use this 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
               }
           }
       }
   }    
}

and call this:

pageCtrl.isPagingEnabled = false
Mohsen Fard
  • 597
  • 9
  • 22