Skip to content

Instantly share code, notes, and snippets.

@koingdev
Created February 6, 2020 15:20
Show Gist options
  • Save koingdev/a5e4ff9edc1b32ad58a8282fde58131e to your computer and use it in GitHub Desktop.
Save koingdev/a5e4ff9edc1b32ad58a8282fde58131e to your computer and use it in GitHub Desktop.
Simple and easy way to show loading indicator inside UIButton in Swift
extension UIButton {
func loadingIndicator(show: Bool, style: UIActivityIndicatorView.Style = .medium, color: UIColor = .white) {
let tag = 10042017
if show {
isEnabled = false
titleLabel?.alpha = 0
let indicator = UIActivityIndicatorView(style: style)
indicator.color = color
indicator.center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
indicator.tag = tag
addSubview(indicator)
indicator.startAnimating()
} else {
isEnabled = true
titleLabel?.alpha = 1
if let indicator = viewWithTag(tag) as? UIActivityIndicatorView {
indicator.stopAnimating()
indicator.removeFromSuperview()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment