Swift/IOS: Change UIButton Background Color
This is a short article trying to clear up on how to change the background color of a UIButton. Depending on your button configuration, you might find 1 way works but not the other.
Before we move onto that, let’s first take a look at the different layers of a UIButton.
You actually have
- a base UIView Layer (red in this case): We will be calling it UIViewBase in this article.
- and on top of that, you have your actual button (yellow background with blue text) which is basically another UIView with some text. Let’s call it UIViewButton.
By that means, to change the background color of a UIButton, we have the following 2 options.
Change the UIViewBase background color
This can simply be achieve by the following.
button.backgroundColor = UIColor.redHowever, if you are finding this method does not affect your UIButton background color, it might because you have already defined a custom Background Configuration in your storyboard with something like following.
This defines the configuration for UIViewButton that obviously comes above the UIViewBase.
Therefore, the above method does work. You just can’t see it!
Get Itsuki’s stories in your inbox
Join Medium for free to get updates from this writer.
You can either use the method we will be covering next, or you can simply set the Background Configuration to Default.
Change the UIViewButton background color
This will be the preferred way if you have a custom Background configuration like what I have shown you above, especially if you have set your corner styles and strokes and you do want to keep all those settings as you change the background color.
To achieve that, we will be
- retrieving the current UIButton.Configuration
- modify just the backgroundColor
- assign it back to the UIButton
var config = button.configuration
config?.background.backgroundColor = UIColor.blue
button.configuration = configI personally don’t see much a difference between the two approaches. So feel free to choose the one you like!
That’s all I have for today!
Happy Tuesday! (And Happy Everyday!)
Stackademic 🎓
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us X | LinkedIn | YouTube | Discord
- Visit our other platforms: In Plain English | CoFeed | Venture | Cubed
- More content at Stackademic.com









