Skip to main content

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 hidden
  • activityIndicator: The activity indicator view that displays during loading state

Methods

  • showLoading(): Activates the loading state by hiding the button text and displaying the activity indicator
  • hideLoading(): Deactivates the loading state by restoring the original text and stopping the activity indicator
  • createActivityIndicator() -> UIActivityIndicatorView: Creates and configures the activity indicator with default settings
  • showSpinning(): Displays the activity indicator with proper Auto Layout constraints
  • centerActivityIndicatorInButton(): 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
}