How To Scroll To A Position Programmatically In UIScrollView

In general we use UIScrollView, UITableView or UICollectionView & sometimes we have to scroll programmatically to Top or bottom, so we can achieve it by using setContentOffset where we set where exactly to scroll

Below are 4 helper methods which help in scrolling the Scroll view

  1. UIScrollView scroll to bottom
  2. UIScrollView scroll to Top
  3. UIScrollView scroll to Right
  4. UIScrollView scroll to Left
//MARK: - Scrolling UIScrollView to possible 4 directions..

extension UIScrollView {
    
    func scrollToBottom() {
        let bottomOffset = CGPoint(x: 0, y: self.contentSize.height - self.bounds.height + self.contentInset.bottom)
        self.setContentOffset(bottomOffset, animated: true)
    }
    
    func scrollToTop() {
        let topOffset = CGPoint.zero
        self.setContentOffset(topOffset, animated: true)
    }
    
    
    func scrollToRightEnd(animated: Bool) {
        if self.contentSize.width < self.bounds.size.width { return }
        let rightOffset = CGPoint(x: self.contentSize.width - self.bounds.size.width, y: 0)
        self.setContentOffset(rightOffset, animated: animated)
    }
    
    func scrollToLeftEnd(animated: Bool) {
        if self.contentSize.width < self.bounds.size.width { return }
        let leftOffset = CGPoint.zero
        self.setContentOffset(leftOffset, animated: animated)
    }
    
}

Example Usage :

DispatchQueue.main.async {
    self.itemsScrollView.scrollToTop()
}

You can Test on This ScrollView project:

6 thoughts on “How To Scroll To A Position Programmatically In UIScrollView”

  1. Please consider adjustedContentInset, which will be more accurate.
    “`
    func scrollToTop(animated: Bool = false) {
    setContentOffset(CGPoint(x: contentOffset.x, y: -adjustedContentInset.top), animated: animated)
    }

    var bottomContentOffsetY: CGFloat {
    max(contentSize.height – bounds.height + adjustedContentInset.bottom, -adjustedContentInset.top)
    }

    func scrollToBottom(animated: Bool = false) {
    setContentOffset(CGPoint(x: contentOffset.x, y: bottomContentOffsetY), animated: animated)
    }

    func scrollToLeading(animated: Bool = false) {
    setContentOffset(CGPoint(x: -adjustedContentInset.left, y: contentOffset.y), animated: animated)
    }

    var trailingContentOffsetX: CGFloat {
    max(contentSize.width – bounds.width + adjustedContentInset.right, -adjustedContentInset.left)
    }

    func scrollToTrailing(animated: Bool = false) {
    setContentOffset(CGPoint(x: trailingContentOffsetX, y: contentOffset.y), animated: animated)
    }

    func scrollViewToVisible(_ view: UIView, animated: Bool = false) {
    scrollRectToVisible(convert(view.bounds, from: view), animated: true)
    }

    var isOnTop: Bool {
    contentOffset.y = bottomContentOffsetY
    }

    }
    “`

    1. There are some problems with the two functions of the above code, allow me to fix it

      var isOnTop: Bool {
      contentOffset.y = bottomContentOffsetY
      }

Leave a Reply to Yang Cancel Reply

Your email address will not be published. Required fields are marked *