Swift Disable horizontal scroll bounce on UIPageController

47 Views Asked by At

I have a Child VC that is being presented over parts of the main ViewController. That Child is a PageViewController, holding two views inside. Swiping left and right to see them each is working great. However, the PageViewController allows users to drag left and right, even if they are on the first or last page. Of course, no other view appears, it just bounces back, but this makes stuff below it visible which I'd like to avoid.

I have implemented the necessary extensions:

func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return pages.count
}

func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    
    guard let firstVC = pageViewController.viewControllers?.first else {
        return 0
    }
    guard let firstVCIndex = pages.firstIndex(of: firstVC) else {
        return 0
    }
    
    return firstVCIndex
}

//Only allow portrait
func pageViewControllerSupportedInterfaceOrientations(_ pageViewController: UIPageViewController) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    
    guard let viewControllerIndex = pages.firstIndex(of: viewController) else { return nil }

    let previousIndex = viewControllerIndex - 1
    guard previousIndex >= 0 else { return nil }
    return pages[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = pages.firstIndex(of: viewController) else { return nil }

    let nextIndex = viewControllerIndex + 1
    guard nextIndex < pages.count else { return nil }
    return pages[nextIndex]
}

Which should avoid looping through the view (which it does indeed). What I now need is to disallow dragging the view further to the right if the user is on the first page, or, further to the left if he is on the last page. I am guessing it is comparable to the bounce effect of a ScrollView. However, I am not finding any function that does this for PageControl.

0

There are 0 best solutions below