Customizing UIButton in iOS 15

Customizing UIButton in iOS 15

By rizwanahmed | ohmyswift | 24 Aug 2021


Buttons are an essential element in iOS apps. If you are developing an app using UIKit, you will probably use UIButton class to create buttons. Creating a button is a straightforward process, but it becomes problematic when it comes to customizations. Soon you will find yourself writing hacks for achieving your desired result. We all have been there, and we have done it. You are not alone. Luckily, iOS 15.0 gives us a new method to customize Buttons much easier using UIButton.Configuration. Let’s get started!

Configuration types in UIButton

UIButton.Configuration comes in four different types, namely plain, filled, gray, and tinted. In this article, we will be focusing only on filled configuration. I will leave the rest of the configurations for you to experiment.

 

var configuration = UIButton.Configuration.filled()

 

Setting a title in a UIButton

Setting a title is done by using the configuration’s title property.

 

configuration.title = "Start download"

 

Setting a title in a UIButton

Setting a subtitle in a UIButton

Again, we can make use of the configuration’s subtitle property to set a subtitle. It would be tough to set a subtitle without the configuration API.

 

configuration.subtitle = "(Downloads a random image)"

 

Setting a subtitle in a UIButton

Changing the background color of a UIButton

The color of the UIButton’s background view can be changed using baseBackgroundColor property.

 

configuration.baseBackgroundColor = .systemOrange

Changing the background color of a UIButton

This article was originally published at ohmyswift.com. Click here to read the full article.

About the author 👨‍💻

Rizwan Ahmed — iOS Engineer.
Twitter 👉 https://twitter.com/rizwanasifahmed
Like our articles? Support us 👉https://www.buymeacoffee.com/ohmyswift

How do you rate this article?

1


rizwanahmed
rizwanahmed

iOS developer. Writes about Swift and iOS development.


ohmyswift
ohmyswift

Weekly articles about Swift and iOS development.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.