How to change UIButton image in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In UIKit, changing a UIButton image usually means calling setImage(_:for:) for a specific control state. The main thing to decide is whether you want a new image for .normal, .highlighted, .selected, or another state, because buttons can show different images depending on interaction state.
The basic API is simple, but confusion usually comes from mixing foreground images with background images, or from expecting tint changes to work when the image is not in template mode.
Set the Image for a Button State
This means:
- when the button is in its normal state, show
play - when
button.isSelectedbecomestrue, showpause
That state-specific behavior is one reason UIButton image changes feel more subtle than changing a plain UIImageView.
To trigger the change:
Change the Image After an Event
A common pattern is to swap the image inside an action method:
This is the simplest answer when the button image should change at runtime because of user interaction.
Use Template Images for Tinting
If you want the image color to follow the button tint, set the image to template rendering mode:
Without template rendering, changing tintColor often has no visible effect because the image keeps its original pixel colors.
setImage Versus setBackgroundImage
These are different APIs:
- '
setImagechanges the foreground icon' - '
setBackgroundImagechanges the stretched background graphic'
Example:
Use the right one for the visual role you mean. Many "why did my image layout look wrong?" bugs come from confusing these two.
Modern Configuration API
On newer UIKit codebases, UIButton.Configuration is also common:
If you later update the configuration image, do it through the configuration object rather than mixing older and newer styling approaches inconsistently.
Working with System Symbols
If you are using SF Symbols instead of PNG assets, the same button APIs still apply:
This is often the cleanest option for standard interface icons because it avoids asset-management overhead and works naturally with tinting.
It also makes state-based image swapping easier, because symbol names are lightweight and integrate well with modern UIKit configuration APIs.
Common Pitfalls
- Changing the image for
.normaland expecting the button to update while it is currently showing another state. - Confusing
setImagewithsetBackgroundImage. - Expecting tint changes to work on an image that is not rendered as a template.
- Using asset names incorrectly and silently getting
nilimages. - Mixing legacy
UIButtonstyling and configuration-based styling in ways that override each other unexpectedly.
Summary
- Use
setImage(_:for:)to change aUIButtonforeground image. - Assign images per control state such as
.normalor.selected. - Use template rendering if the button tint should recolor the image.
- Use
setBackgroundImageonly for background graphics, not icon swaps. - Make sure you are updating the image for the state the button is actually displaying.
Related reading
- How to change UIButton image in Swift
- How to change UINavigationBar background color from the AppDelegate
- How to change UIPickerView height
- How to change uitableview delete button text
- how to change uiviewcontroller title independent of tabbar item title
- How to check current thread in Swift 3?
- How to check current thread in Swift 3?
- How to check for Dark Mode in iOS?
.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.