UIButton
imageView
iOS development
Swift programming
UI scaling

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:

swift
1import UIKit
2
3let button = UIButton(type: .system)
4button.setImage(UIImage(named: "icon-star"), for: .normal)
5button.frame = CGRect(x: 40, y: 80, width: 80, height: 80)
6button.imageView?.contentMode = .scaleAspectFit

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:

swift
1import UIKit
2
3let button = UIButton(type: .system)
4button.setImage(UIImage(named: "icon-star"), for: .normal)
5button.frame = CGRect(x: 40, y: 80, width: 80, height: 80)
6button.imageView?.contentMode = .scaleAspectFit
7button.contentEdgeInsets = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)

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:

swift
1button.translatesAutoresizingMaskIntoConstraints = false
2
3NSLayoutConstraint.activate([
4    button.widthAnchor.constraint(equalToConstant: 80),
5    button.heightAnchor.constraint(equalToConstant: 80)
6])

Size SF Symbols with a symbol configuration

For SF Symbols, scale the symbol itself rather than treating it as a generic image:

swift
1import UIKit
2
3let button = UIButton(type: .system)
4let config = UIImage.SymbolConfiguration(pointSize: 28, weight: .medium)
5let image = UIImage(systemName: "heart.fill", withConfiguration: config)
6
7button.setImage(image, for: .normal)
8button.tintColor = .systemRed

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:

swift
button.imageView?.frame = CGRect(x: 0, y: 0, width: 20, height: 20)

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:

swift
1import UIKit
2
3var config = UIButton.Configuration.plain()
4config.image = UIImage(systemName: "paperplane.fill")
5config.imagePadding = 8
6
7let button = UIButton(configuration: config)
8button.preferredSymbolConfigurationForImage = UIImage.SymbolConfiguration(pointSize: 24)

This works especially well when the button also has a title and you want the system to manage the image, title and padding together.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms