Search code examples
swiftuipageviewcontroller

How to disable Scroll left/right on UIPageViewController?


I have been researching this all day, there are many posts about this but I haven't managed to find any that are helpfull for me!

I have a UIPageViewController which displays two views(It is not a root view).

Basically when I add a function from one of those views inside the UIPageViewController I would like to be able to disable the scrolling...?

Is that possible in any way?!

Many thanks in advance to anyone that help!


Solution

  • Just found the solution!

    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)