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
Changing a UIButton image in Swift is a common UI requirement for toggles, playback controls, favorites, and stateful actions. It looks simple, but reliable implementations need to handle control states (normal, highlighted, selected, disabled), asset sizing, rendering mode, and async updates.
If image updates are done ad hoc in multiple places, buttons can show the wrong icon after reuse, state transitions, or theme changes. A better approach is to map states clearly and update image + accessibility together.
Core Sections
1. Set images by control state
Use setImage(_:for:) to assign an icon for each relevant state.
This keeps behavior predictable: toggling isSelected switches icon automatically.
2. Use SF Symbols and rendering configuration
For modern iOS, SF Symbols plus symbol configuration gives consistent visual scale.
If your asset should keep original colors, set rendering mode:
Without this, system tinting may recolor the icon unexpectedly.
3. Update images safely during async workflows
When icons depend on remote state, update UI on the main thread and avoid race conditions.
If your button appears inside reusable cells, reset state in prepareForReuse and rebind state during configuration to avoid stale images.
For iOS 15+, UIButton.Configuration can simplify consistent image/text spacing:
Common Pitfalls
- Setting only the
.normalimage and expecting automatic updates forselectedordisabledstates. - Updating button images from background threads after network calls.
- Using template images unintentionally and getting unexpected tint colors.
- Forgetting to synchronize
isSelectedwith underlying model state, causing UI drift. - Not updating accessibility labels/hints when image meaning changes.
Summary
Changing a UIButton image in Swift is most reliable when you treat it as state mapping, not one-off assignment. Define icons per control state, use symbol or rendering configuration intentionally, and perform async UI updates on the main thread. Pair visual updates with accessibility text so behavior is clear for all users.
For consistent visual behavior, define button imagery through design tokens or configuration structs rather than scattering literal asset names. A small ButtonIconSet model can map normal/selected/disabled images and accessibility labels together. This keeps state handling centralized and avoids stale references when assets are renamed. It also helps teams support theming (light, dark, brand variants) without touching business logic.
When debugging image updates that "do nothing," inspect content insets, image edge insets, and Auto Layout constraints. Sometimes the image is set correctly, but clipping or layout compression hides it. In reusable views like table or collection cells, always reset button state during reuse and reapply model state during configure. That pattern eliminates most phantom UI-state bugs.
Automated UI tests that tap stateful buttons and assert resulting icons can catch regressions early, especially after design-system or asset updates.
If the same icon appears in many screens, wrap setup in a helper method so state-image mapping remains consistent and easier to update during redesigns.
Related reading
- 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?
- How to check if a file exists in the Documents directory in Swift?
.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.