UIScrollView not scrolling
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When a UIScrollView refuses to scroll, the problem is usually not mysterious. In most cases, the scroll view simply has no larger content area to scroll, or Auto Layout never produced the correct content size.
The fix depends on how the scroll view is built. If you lay it out manually, check contentSize. If you use Auto Layout, check the constraints attached to contentLayoutGuide and frameLayoutGuide.
The First Rule: The Content Must Be Bigger Than the Frame
A scroll view only scrolls when its content area is larger than its visible frame. If the content is the same height and width as the viewport, there is nothing to scroll.
In manual layout code, that means setting contentSize correctly:
If contentSize.height is not greater than the scroll view height, vertical scrolling will not happen.
Auto Layout Needs Complete Constraints
In modern iOS code, the most common cause is incomplete Auto Layout setup. The recommended pattern is:
- pin the scroll view itself into the parent view
- pin the content view to the scroll view's
contentLayoutGuide - constrain the content view width to the scroll view's
frameLayoutGuidefor vertical scrolling
Example:
That bottom constraint is especially important. Without it, Auto Layout often cannot infer the full scrollable height.
Common Real Causes
If a scroll view is not scrolling, check these in order:
- the content is not actually taller or wider than the viewport
- the bottom or trailing content constraint is missing
- the content view width is not tied correctly, so the layout is ambiguous
- another gesture recognizer or parent view is intercepting touches
- the scroll view itself has zero or incorrect frame size
Many "scrolling bugs" are really layout bugs.
Nested Scroll Views and Stack Views
Problems get more common when you place a stack view inside a scroll view or nest scrollable controls. The stack view itself does not make scrolling happen. It still needs complete constraints so the scroll view can derive the full content size.
Likewise, if you embed a table view or collection view inside another scroll view, gesture handling and content sizing become more complicated. Often the cleaner solution is to use one main scrolling container instead of stacking several scrollable views.
Common Pitfalls
- Forgetting that
contentSizemust exceed the visible frame for scrolling to occur. - Missing the bottom constraint between content and
contentLayoutGuide. - Pinning content directly to the scroll view frame instead of using the layout guides correctly.
- Assuming a
UIStackViewinside the scroll view automatically creates scrollable height. - Debugging gestures first when the real problem is that the content simply is not larger than the viewport.
Summary
- A
UIScrollViewscrolls only when its content area is larger than its frame. - Manual layout requires a correct
contentSize. - Auto Layout requires complete constraints to
contentLayoutGuideand usually a width constraint toframeLayoutGuide. - Missing bottom constraints are a very common cause.
- When a scroll view does not scroll, verify layout and content size before chasing gesture issues.
Related reading
- UIScrollView paging horizontally, scrolling vertically?
- UIScrollView pauses NSTimer until scrolling finishes
- UIScrollView scroll to bottom programmatically
- UIScrollView Scrollable Content Size Ambiguity
- UIScrollView Scrollable Content Size Ambiguity
- UIStatusBarStyle not working in Swift
- UISearchBar increases navigation bar height in iOS 11
- UISegmentedControl below UINavigationbar in iOS 7
.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.