Programmatically scroll a UIScrollView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIScrollView is normally driven by touch, but many iOS interfaces need to move it in code. Typical cases include jumping to an error message, revealing the active text field when the keyboard appears, restoring a saved position, or paging to a selected item. The core API is simple, but correct scrolling depends on layout timing and content insets.
The Three Properties That Matter
Programmatic scrolling usually revolves around three values:
- '
contentSize, which is the total scrollable area' - '
bounds, which is the visible region' - '
contentOffset, which is the current scroll position'
The most direct way to scroll is to set the offset:
This moves the visible origin to the requested point. Using animation is usually better for user-facing interactions because it preserves visual context.
Scrolling to the Top or Bottom
When you want a well-defined edge position, include the adjusted insets rather than using hard-coded coordinates.
That makes the behavior safer when safe areas, navigation bars, or automatic content inset adjustment are involved.
Scrolling a Subview Into View
Often the goal is not a raw offset but a particular control. In that case, convert the subview's rect into the scroll view's coordinate system and use scrollRectToVisible.
This is especially useful for forms, validation messages, and focused input controls because it avoids manual offset math.
Wait Until Layout Is Finished
A very common source of incorrect scrolling is calculating the target position too early. Before Auto Layout finishes, frames and content size may still be wrong.
viewDidLayoutSubviews is often a better choice than viewDidLoad when the target depends on final geometry.
Keeping a Field Visible When the Keyboard Appears
One of the most common reasons to scroll programmatically is keeping the active text field visible above the keyboard. A standard pattern is to update the inset and then reveal the focused field.
This is usually better than shifting the whole view controller, because the scroll behavior stays attached to the scrollable content.
Paging Horizontally
For paged horizontal content, compute the offset from the page width:
This works especially well with isPagingEnabled = true.
Common Pitfalls
The most common mistake is trying to scroll before layout is complete. If frames are stale or zero, the calculated offset will be wrong.
Another problem is ignoring insets. Safe areas, keyboards, and navigation bars all affect the usable visible region, so hard-coded offsets often fail on real devices.
People also try to scroll beyond the valid range. UIScrollView clamps many values, but bad math still produces visually incorrect results.
Finally, do not confuse the scroll view's coordinate system with a subview's coordinate system. Convert rectangles before using them.
Summary
- Use
setContentOffsetwhen you know the exact point to reveal. - Use
scrollRectToVisiblewhen the target is a specific subview. - Wait until layout is finished before calculating frames or offsets.
- Include
adjustedContentInsetin top and bottom calculations. - For forms, combine keyboard inset updates with scrolling the active field into view.
Related reading
- Programmatically Select all text in UITextField
- Programmatically selecting text in an input field on iOS devices mobile Safari
- Programmatically set image to UIImageView with Xcode 6.1/Swift
- Programmatically set left drawable in a TextView
- Programmatically set the initial view controller using Storyboards
- Programmatically set the initial view controller using Storyboards
- Programmatically switching between tabs within Swift
- Programming with SurfaceView and thread strategy for game development
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.