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!
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 is done by using the configuration's title property.
configuration.title = "Start download"
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)"
Adding an image to the button and customizing it has become effortless. An important point to note here is that, the image respects the language's direction.
configuration.image = UIImage(systemName: "arrow.down.square.fill")
The UIButton configuration makes it easy to set a padding between the image and the button's
configuration.imagePadding = 20
The imagePlacement is my favorite property. I have written so many nasty hacks for just changing the position of the image in a UIButton, before iOS 15.0
The imagePlacement property supports the following options - leading, trailing, top, or bottom.
configuration.imagePlacement = .trailing