How to scale a UIButton's imageView and image size
Codemia Knowledge Hub ·
Control how the image fits inside the button's content area instead of resizing the internal imageView frame. UIButton manages that image view itself, so set the image, pick the right content mode, and size the button or the image asset. Fighting the button's internal layout directly gives fragile results.
Set the image and content mode
For a normal raster image, make the embedded image view scale proportionally:
scaleAspectFit is the safest default because it preserves the aspect ratio. scaleAspectFill fills more of the area if some cropping is acceptable. scaleToFill usually distorts icons, so use it only deliberately.
Scaling cannot invent detail. If the source asset is tiny or low quality, no layout change will fix how it looks.
Shrink the image with insets and button size
Often the real issue is how much room the image gets inside the button. contentEdgeInsets adds padding so the image draws smaller within the same bounds:
The image file is unchanged; the button simply gives it a smaller drawing area. When the goal is just a smaller-looking image, padding and button sizing are more reliable than micromanaging the image view.
With Auto Layout, size the button with constraints instead of a frame:
Size SF Symbols with a symbol configuration
For SF Symbols, scale the symbol itself rather than treating it as a generic image:
The symbol renders at the requested point size, which looks much cleaner than stretching it.
Why setting imageView.frame does not stick
It is tempting to write:
This usually does not hold, because UIButton recalculates its subview layout during state changes and layout passes and overwrites your frame. Influence the layout through supported APIs instead: content mode, edge insets, constraints and image selection.
Using UIButton.Configuration
In newer UIKit code, UIButton.Configuration makes image sizing and spacing more predictable:
This works especially well when the button also has a title and you want the system to manage the image, title and padding together.
Related reading
- How do I scroll the UIScrollView when the keyboard appears?
- How do I see if Wi-Fi is connected on Android?
- How do I see which version of Swift I'm using?
- How do I "select Android SDK" in Android Studio?
- How do I set a weight to SF Symbols for iOS 13?
- How do I Set Background image in Flutter?
- How do I set bold and italic on UILabel of iPhone/iPad?
- How do I set the height of tableHeaderView UITableView with autolayout?
Course lessons on this
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.