Zoom unscaled views in UIScrollView to bounds
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When you want content inside a UIScrollView to fit the visible bounds initially, the key is to compute a minimum zoom scale that matches the scroll view’s size. After that, you center the content with insets so the view looks correct both when it fits exactly and when it becomes smaller than the viewport.
Core Sections
Start with a dedicated zoomable content view
A scroll view zooms one subview, not an abstract concept of “all content.” In practice that means you usually embed an image view or container view inside the scroll view and return that view from viewForZooming(in:).
That wiring is required before zoom configuration does anything useful.
Compute the fit-to-bounds scale
To show the content uncut inside the scroll view, compare the scroll view size to the content size and choose the smaller scaling ratio.
Using min(xScale, yScale) ensures the entire content fits inside the visible bounds without cropping.
Center the content when it becomes smaller than the viewport
After zooming out, the zoomed view may be smaller than the scroll view. Without extra work, it sticks to the top-left corner. The usual fix is to adjust contentInset based on the zoomed frame size.
Call this in scrollViewDidZoom(_:) and after setting the initial scale.
Update on layout changes
Rotation and safe-area changes can invalidate the fit-to-bounds calculation. Recompute scales in viewDidLayoutSubviews() once the scroll view has its final size.
If you skip this, the initial zoom may look correct in one orientation and wrong after rotation.
Distinguish “fit to bounds” from “actual size”
The phrase “unscaled” can be misleading. If the content is larger than the viewport, fitting it to the bounds requires scaling it down. If you instead want one image pixel to map as directly as possible to screen points, that is a different goal and may require a zoom scale of 1.0 rather than the computed fit scale.
So the first question is: do you want “entire content visible,” or do you want “display at intrinsic scale”? Those are not always the same thing.
Common Pitfalls
- Forgetting to implement
viewForZooming(in:), which means the scroll view never actually knows which subview should zoom. - Setting zoom scales before the scroll view has its final bounds, which leads to wrong fit calculations on first layout or after rotation.
- Computing the fit scale correctly but never centering smaller content, so it stays pinned to the top-left corner.
- Assuming “unscaled” and “fit to bounds” mean the same behavior even when the content is larger than the viewport.
- Updating only
zoomScaleand forgetting to keepcontentSizeand layout assumptions consistent with the zoomable view.
Summary
- Put the zoomable content in a dedicated subview and return it from
viewForZooming(in:). - Compute the minimum zoom scale from the scroll view bounds and content size.
- Center content with insets when the zoomed view is smaller than the viewport.
- Recalculate zoom behavior after layout changes such as rotation.
- Decide clearly whether you want fit-to-bounds behavior or true intrinsic-scale display.
Related reading
- Zooming MKMapView to fit annotation pins?
- -1103 Error DomainNSURLErrorDomain Code-1103 resource exceeds maximum size iOS 13
- Use the inherited flag, or Remove the build settings from the target. CocoaPod Swift3 pod update error
- -didSelectRowAtIndexPath not being called
- 2D cross-platform game engine for Android and iOS?
- 2D cross-platform game engine for Android and iOS?
- Why is the Android emulator so slow? How can we speed up the Android emulator?
- ✔ Checkmark selected row in UITableViewCell
.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.