Fix: UIScrollView Not Scrolling to Top in Swift and UIKit

Written by

in

Quick answer: To scroll a UIScrollView to the top, set its content offset to CGPoint(x: 0, y: -contentInset.top) with animation. This accounts for the top content inset (like safe area or navigation bar). A simple Swift extension makes this a one-liner: scrollView.scrollToTop().

Why this happens

UIScrollView’s coordinate system places the content origin at (0,0) relative to the scroll view’s bounds. However, when a scroll view has a top content inset—common with navigation bars, safe areas, or custom insets—the visible content starts at y = -contentInset.top. Setting the content offset to CGPointZero positions the scroll view so that the top inset area is visible, not the actual content. This is why simply calling setContentOffset(.zero, animated: true) often appears to stop short of the true top.

Additionally, the scrollsToTop gesture (tapping the status bar) only works when exactly one scroll view in the view hierarchy has scrollsToTop = true. If multiple scroll views have it enabled, the system ignores the gesture.

How to fix it

1. Use the correct content offset with contentInset.top (Recommended)

The most robust solution accounts for the current top content inset. This works regardless of whether the inset comes from the safe area, a navigation bar, or manual adjustments.

extension UIScrollView {
    /// Scrolls the content to the very top, respecting the current top content inset.
    /// - Parameter animated: Whether to animate the scrolling. Defaults to true.
    func scrollToTop(animated: Bool = true) {
        // The visual top of the content is at negative top inset.
        let targetOffset = CGPoint(x: 0, y: -contentInset.top)
        setContentOffset(targetOffset, animated: animated)
    }
}

Usage:

// Animate to top
tableView.scrollToTop()

// Without animation
collectionView.scrollToTop(animated: false)

Why this works: contentInset.top reflects the current inset, including any adjustments from adjustedContentInset (iOS 11+). Using -contentInset.top aligns the content’s top edge with the scroll view’s top visible edge.

2. Preserve horizontal scroll position

When your scroll view supports horizontal scrolling (e.g., a paging carousel), you may want to reset only the vertical position.

extension UIScrollView {
    func scrollToTopPreservingHorizontal(animated: Bool = true) {
        let targetOffset = CGPoint(x: contentOffset.x, y: -contentInset.top)
        setContentOffset(targetOffset, animated: animated)
    }
}

3. Leverage the system scroll-to-top gesture

UIScrollView provides a built-in behavior: tapping the status bar scrolls the scroll view to the top. This requires:
– scrollsToTop = true (default)
– The scroll view must be in the view hierarchy of a view controller.
– Only one scroll view per view controller should have this enabled; otherwise, the gesture does nothing.

// Enable (usually default)
scrollView.scrollsToTop = true

// Disable on secondary scroll views to avoid conflicts
secondaryScrollView.scrollsToTop = false

Note: This gesture is disabled when the scroll view is already at the top, or when the user is actively scrolling.

4. Scroll to top after content reloads

A common scenario: after reloading a table or collection view, you want to jump to the top. Call the method after the layout pass:

tableView.reloadData()
tableView.layoutIfNeeded() // Ensure insets are up to date
tableView.scrollToTop()

If you need to wait for the next run loop (e.g., after inserting rows), wrap in DispatchQueue.main.async.

How to prevent it

  • Always use -contentInset.top instead of 0 for the vertical offset.
  • Set scrollsToTop = false on all but the primary scroll view in a view controller.
  • Call layoutIfNeeded() before scrolling if you just changed insets or constraints.
  • Avoid calling scrollToTop() inside viewDidLoad before the view hierarchy is laid out; use viewDidLayoutSubviews or viewDidAppear instead.
  • For programmatic scrolling during animations, consider UIView.animate(withDuration:) with layoutIfNeeded for custom transitions.

FAQ

Why does setContentOffset(.zero, animated: true) not scroll to the very top?

Because .zero corresponds to the top of the scroll view’s bounds, not the top of the content. When a top content inset exists (from safe area, navigation bar, or custom inset), the content’s top edge is visually positioned at y = -contentInset.top. Setting offset to zero leaves that inset area visible.

Can I scroll to top without animation?

Yes. Pass animated: false to setContentOffset or use the extension method with animated: false. The content jumps immediately.

Does this work with UICollectionView and UITableView?

Yes. Both UITableView and UICollectionView inherit from UIScrollView, so the same extension applies. For table views, you can also use scrollToRow(at:at:animated:) with index path [0,0], but the content offset method is simpler and works for any scroll view.

What about SwiftUI’s ScrollView?

SwiftUI’s ScrollView doesn’t expose a direct content offset API. To scroll to top in SwiftUI, use a ScrollViewReader with a proxy:

ScrollViewReader { proxy in
    ScrollView {
        // content
    }
    .onAppear {
        proxy.scrollTo("TOP_ID", anchor: .top)
    }
}

Attach an .id("TOP_ID") to the topmost view.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.