Creating a UIImage from a UIColor to use as a background image for UIButton
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.
Introduction
Using a color image for UIButton backgrounds is still useful even with modern APIs, especially when you need per-state visuals, stretchable assets, or compatibility with older code paths. Instead of shipping many static image files, you can generate tiny images from UIColor at runtime. This article shows a reliable pattern that works well in production.
Generate a Solid Color Image in Swift
The core idea is simple: draw a one pixel image with a color, then reuse it as the button background. UIGraphicsImageRenderer is the modern and safe choice.
This is fully runnable in an iOS app target. The default size of one by one is enough for flat colors and keeps memory use minimal.
Apply State-Specific Backgrounds on UIButton
A key benefit of using images is button state control. You can set a different image for normal, highlighted, and disabled states.
With this setup, state transitions are clear and do not require extra image assets in your bundle.
Handle Stretching and Rounded Designs
If you draw a larger source image with gradients or borders, use cap insets so resizing does not distort corners.
Then set it as a background image and the shape will scale cleanly on different button widths.
Dynamic Colors and Theme Changes
If your app supports light mode and dark mode, prefer dynamic colors like systemBlue. When trait collection changes, regenerate images for accurate appearance.
This avoids stale images after theme toggles.
iOS 15 and UIButton.Configuration
On modern iOS versions, UIButton.Configuration can handle many style cases without background images. Still, image based backgrounds remain useful when you need exact rendering parity with legacy screens. You can combine both approaches: configuration for text and spacing, generated images for background states.
This hybrid pattern eases migration in large codebases where parts of the UI still depend on state images.
Common Pitfalls
One frequent issue is using setBackgroundColor for stateful behavior. UIButton does not provide per-state background color directly, so visuals appear inconsistent during highlight. Another issue is generating large images for flat fills, which wastes memory and can increase rendering cost. A third mistake is forgetting masksToBounds when applying corner radius, causing the image to bleed beyond rounded edges. Finally, with dynamic colors, many teams forget to refresh generated images on appearance changes. That can produce mismatched colors between labels and backgrounds.
Summary
- Generate a tiny image from
UIColorusingUIGraphicsImageRenderer. - Use
setBackgroundImagefor per-state button styling. - Use resizable images with cap insets for rounded or bordered backgrounds.
- Rebuild generated images when light or dark appearance changes.
- Keep source image sizes small for better memory and rendering behavior.
Related reading
- Creating and playing a sound in swift
- Creating iOS/OSX Frameworks is it necessary to codesign them before distributing to other developers?
- Creating layout constraints programmatically
- Creating NSData from NSString in Swift
- Creating temporary files in Android
- Cropping an UIImage
- Cursor not showing up in UITextView
- Custom Adapter for List View
.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.