Get UIScrollView to scroll to the top
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Programmatically moving a UIScrollView to the top is common in feed screens, search resets, and tab reselect behavior. The implementation is easy, but correct top position depends on content insets and safe area adjustments. A reliable solution computes offset from adjustedContentInset instead of assuming zero.
Compute the Real Top Offset
If navigation bars or safe area are active, visible top is not y = 0. Use this formula:
- top y equals negative adjusted top inset.
This works for plain UIScrollView, UITableView, and UICollectionView because they share the same base class.
Button and Tab Reselect Triggers
Typical pattern is a floating button that appears after some scroll distance.
For tab reselect behavior, call the same helper from tab controller delegate callback when user taps current tab again.
Using Index-Based APIs for List Views
For UITableView and UICollectionView, scrolling to first item can be clearer than raw offsets when sections are dynamic.
Collection variant:
Index-based calls are useful when layout has custom headers or content inset transitions.
Status Bar Tap and scrollsToTop
iOS supports status bar tap to jump to top. It only works correctly when one visible scroll view has scrollsToTop = true.
If multiple scroll views are set to true, tap behavior may fail or feel random.
Keyboard and Safe Area Timing
When keyboard appears, content insets can change asynchronously. If you scroll before layout settles, top offset may be wrong.
Practical approach:
- Update constraints and insets.
- Call
view.layoutIfNeeded(). - Then perform
scrollToTop.
For chat screens, you may choose not to auto-scroll to top at all and instead preserve user position.
UX Guidance
Auto-jumping users to top can be disruptive. Prefer explicit actions:
- Back-to-top button.
- Tab reselect contract.
- Pull-to-refresh completion only when user is already near top.
Add animation for user-initiated actions and skip animation for state restore on view re-entry.
Common Pitfalls
- Using
CGPoint.zeroas universal top. Fix by using negativeadjustedContentInset.top. - Calling top scroll before layout and inset updates. Fix by deferring until layout is stable.
- Enabling
scrollsToTopon multiple visible scroll views. Fix by allowing only one active view. - Forcing top jump after every reload. Fix by preserving context unless user explicitly requests jump.
- Not handling empty lists in index-based scrolling. Fix by checking item counts first.
Summary
- Reliable top scrolling in iOS depends on insets, not raw zero offset.
- One helper function can support scroll view, table view, and collection view.
scrollsToToprequires single ownership in visible hierarchy.- Layout timing and keyboard changes affect scroll correctness.
- Prefer intentional UX triggers instead of surprise automatic jumps.

