Skip to content

Instantly share code, notes, and snippets.

@sukov
Last active June 22, 2021 15:10
Show Gist options
  • Save sukov/b4949987bfe96754575265c740aea32f to your computer and use it in GitHub Desktop.
Save sukov/b4949987bfe96754575265c740aea32f to your computer and use it in GitHub Desktop.
UIButton loading indicator
extension UIButton {
/// Unique number used to tag the loading indicator view
private var loadingIndicatorTag: Int {
808404
}
private var loadingIndicator: UIActivityIndicatorView? {
viewWithTag(loadingIndicatorTag) as? UIActivityIndicatorView
}
func displayLoadingIndicator() {
isEnabled = false
alpha = 0.8
let indicator = UIActivityIndicatorView(style: .medium)
indicator.color = .white
let buttonHeight = self.bounds.size.height
let buttonWidth = self.bounds.size.width
let titleLabelMaxX = titleLabel?.frame.maxX ?? 0
indicator.center = CGPoint(x: titleLabelMaxX + (buttonWidth - titleLabelMaxX) / 2, y: buttonHeight / 2)
indicator.tag = loadingIndicatorTag
addSubview(indicator)
indicator.startAnimating()
}
func hideLoadingIndicator() {
isEnabled = true
alpha = 1.0
loadingIndicator?.stopAnimating()
loadingIndicator?.removeFromSuperview()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment