How to add a loader to an UIButton
• Rizwan Ahmed A • UIButton • add loader to an uibutton, swift, UIButton, iOS15, UIButtonConfiguration, ios15 button configuration, swift ios 15, customizing uibutton in swift, swift uibutton, implementing button in swift
Sometimes, we would require a button to show a loader with appropriate text when a long-duration task is done, such as downloading an image. Before iOS 15, we had to write a custom button or do some hacks to show a simple loader or an UIActivityIndicator inside an UIButton. Now, it is not the case anymore. UIButtonConfiguration provides showsActivityIndicator
, a simple property using which we can hide/show a loader inside a button.
We recommend that you visit this article if you are unfamiliar with UIButtonConfiguration.
To hide the loader or activity indicator
To show the loader or activity indicator
Customizing the color of the loader or activity indicator
We can also customize the color of the loader or activity indicator by using the UIConfigurationColorTransformer
.
We can use two options: choose the default ones (grayscale, preferredTint, or monochromeTint) or use our custom color.
Default color usage
Custom color usage
The UIButton provides us a configurationUpdateHandler
where we can change the button’s appearance using the configuration. The configurationUpdateHandler
is triggered when the setNeedsUpdateConfiguration
method is called on the button.
References
[1] https://developer.apple.com/documentation/uikit/uibutton/configuration
About the author
- Rizwan Ahmed - iOS Engineer. Twitter - https://twitter.com/rizwanasifahmed
Support creators
More articles
- How to filter screenshots, cinematic videos, and depth-effect photos in PHPickerViewController
- Implementing a custom native calendar using UICalendarView in iOS16 and Swift
- Exploring Deque in Swift Collections
- Replacing UIImagePickerController with PHPickerViewController
- Embracing Localization in Image Assets
- An effective way to clear entire Userdefaults in Swift
Like our articles? Support us!