Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable UIPageViewController Swipe - Swift

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

like image 314
Voyager Avatar asked Aug 02 '16 02:08

Voyager


2 Answers

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)
like image 188
Rurouni Avatar answered Nov 15 '22 09:11

Rurouni


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
like image 41
Mohsen Sedaghat Fard Avatar answered Nov 15 '22 08:11

Mohsen Sedaghat Fard