Search code examples
iosiphoneswiftuipageviewcontrollerbounce

How to remove UIPageViewController's Bounce effect in iOS for Swift 3/4


Here is the screenshot of my issue:

enter image description here


Solution

  • First find scrollview inside your UIPageViewController and add UIScrollViewDelegate

    for view in self.pageViewController!.view.subviews {
            if let subView = view as? UIScrollView {
                subView.delegate = self
                subView.isScrollEnabled = true
                subView.bouncesZoom = false
    
            }
    }
    

    (Extends UIScrollViewDelegate in your ViewController class)

    For UIScrollViewDelegate on method "scrollViewDidScroll" and "scrollViewWillEndDragging" you can add something like this

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
        if(currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width){
            scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
        }else if(currentIndex == 2 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
        }
    }
    
    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        if(currentIndex == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width){
            scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
        }else if(currentIndex == 2 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPoint(x:scrollView.bounds.size.width, y:0.0)
        }
    }