LMLoadingButton
Overview
The LMLoadingButton
is a specialized button widget that extends LMButton
with built-in loading state management. It provides seamless transitions between normal and loading states, automatically handling the display of an activity indicator and text state changes. This component is ideal for operations that require user feedback during asynchronous processes.
File Location:
LMButton.swift
Functionality
UI Components
originalButtonText
: Stores the original button text that will be restored when loading state is hiddenactivityIndicator
: The activity indicator view that displays during loading state
Methods
showLoading()
: Activates the loading state by hiding the button text and displaying the activity indicatorhideLoading()
: Deactivates the loading state by restoring the original text and stopping the activity indicatorcreateActivityIndicator() -> UIActivityIndicatorView
: Creates and configures the activity indicator with default settingsshowSpinning()
: Displays the activity indicator with proper Auto Layout constraintscenterActivityIndicatorInButton()
: Centers the activity indicator within the button using constraints
Customization
CustomLoadingButton.swift
class CustomLoadingButton: LMLoadingButton {
override init(frame: CGRect) {
super.init(frame: frame)
setupDefaultAppearance()
}
private func setupDefaultAppearance() {
backgroundColor = .systemBlue
setTitleColor(.white, for: .normal)
layer.cornerRadius = 12
addShadow()
setFont(.systemFont(ofSize: 16, weight: .semibold))
}
override func createActivityIndicator() -> UIActivityIndicatorView {
let activityIndicator = UIActivityIndicatorView(style: .large)
activityIndicator.hidesWhenStopped = true
activityIndicator.color = .white
return activityIndicator
}
override func showLoading() {
super.showLoading()
// Add custom loading behavior
}
override func hideLoading() {
super.hideLoading()
// Add custom completion behavior
}
}
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
LMUIComponents.shared.customLoadingButton = CustomLoadingButton.self
// ...
return true
}