UIButton won't go to Aspect Fit in iPhone
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A common iOS UI frustration is setting a button image to aspect fit and seeing no visible change. Developers often set button.imageView?.contentMode = .scaleAspectFit and expect the icon to resize correctly, but the image still appears stretched, clipped, or centered with incorrect padding. This happens because UIButton layout is influenced by several layers: the button frame, edge insets, image view frame, Auto Layout constraints, and on modern iOS, UIButton.Configuration behavior.
In other words, contentMode alone is rarely enough. You need to control the image container size and the button’s layout model together. Once those constraints are explicit, aspect fit works consistently across device sizes and Dynamic Type changes.
Core Sections
1. Use the correct API for your button style
For iOS 15+, prefer UIButton.Configuration; for older code paths, configure the embedded image view directly.
With configuration-based buttons:
Do not mix old inset APIs and configuration APIs blindly; configuration may override legacy properties.
2. Constrain button dimensions to a realistic image box
Aspect fit preserves proportions inside available bounds. If the image view gets a tiny or ambiguous frame, the result looks wrong even with correct mode.
If the icon should dominate the button, allocate more vertical space and reduce title impact or remove the title.
In stack views, low hugging/compression priorities can collapse button height unexpectedly, which makes the image appear cropped.
3. Debug layout with runtime inspection
Inspect actual frames after layout:
If imageView frame is not what you expect, check conflicting constraints or state-specific configuration updates. It is common to set the image for .normal only and forget .highlighted or .selected, then think aspect fit is inconsistent.
4. Handle SF Symbols and bitmap assets differently
SF Symbols scale with point size and weight. Bitmap assets depend on pixel dimensions and rendering mode. For symbols, control symbol configuration:
For bitmap assets, provide correctly sized @2x/@3x resources. Aspect fit cannot fix low-resolution images that are too small for the target frame.
Common Pitfalls
- Setting
imageView.contentModewithout giving the button a stable frame through constraints. - Mixing
UIButton.Configurationand legacy insets/properties, causing hidden overrides. - Forgetting state-specific images, so pressed or selected states use different sizing behavior.
- Expecting aspect fit to repair poor source assets instead of supplying proper image dimensions.
- Embedding the button in stack views where compression priorities shrink it below usable icon size.
Summary
When a UIButton refuses to behave like aspect fit, the issue is usually layout ownership, not the aspect mode itself. Define button size explicitly, use a single configuration model, and verify the actual imageView frame at runtime. Separate handling for SF Symbols versus bitmap assets, and ensure all control states are configured consistently. With those constraints in place, .scaleAspectFit works predictably across iPhone sizes and avoids the stretched-or-clipped icon issues that make UI polish difficult.
A practical way to keep this issue from returning is to turn the fix into a lightweight runbook. Capture the exact environment assumptions (tool versions, runtime flags, cluster or platform settings, and required dependencies), then store a short verification command sequence that any teammate can run from a clean setup. This makes troubleshooting deterministic instead of person-dependent and reduces rework during on-call incidents.
It also helps to add one automated guardrail in CI or pre-deploy checks that validates the critical assumption described above. That guardrail might be a linter rule, a smoke test, a schema check, a policy validation step, or a minimal integration test. When the same class of failure is caught before release, teams spend less time on emergency debugging and more time on controlled improvements.
Related reading
- UICollectionReusableView method not being called
- UICollectionView - dynamic cell height?
- UICollectionView - Horizontal scroll, horizontal layout?
- UICollectionView animate data change
- UICollectionView auto scroll to cell at IndexPath
- UICollectionView current visible cell index
- UICollectionView flowLayout not wrapping cells correctly
- UICollectionView, full width cells, allow autolayout dynamic height?
.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.