Skip to content

Instantly share code, notes, and snippets.

@nguyentruongky
Created April 24, 2019 10:44
Show Gist options
  • Save nguyentruongky/0e9a3ec53fe5e5fb5f9ba64328b2db79 to your computer and use it in GitHub Desktop.
Save nguyentruongky/0e9a3ec53fe5e5fb5f9ba64328b2db79 to your computer and use it in GitHub Desktop.
Add loading indicator with color, size to any views
extension UIView {
static let loadingViewTag = 1938123987
func showLoading(style: UIActivityIndicatorView.Style = .gray, color: UIColor? = nil, scale: CGFloat = 1) {
var loading = viewWithTag(UIView.loadingViewTag) as? UIActivityIndicatorView
if loading == nil {
loading = UIActivityIndicatorView(style: style)
}
loading?.scale(value: scale)
if let color = color {
loading?.color = color
}
loading?.translatesAutoresizingMaskIntoConstraints = false
loading!.startAnimating()
loading!.hidesWhenStopped = true
loading?.tag = UIView.loadingViewTag
addSubview(loading!)
loading?.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
loading?.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
}
func stopLoading() {
let loading = viewWithTag(UIView.loadingViewTag) as? UIActivityIndicatorView
loading?.stopAnimating()
loading?.removeFromSuperview()
}
}
@takasurazeem
Copy link

Value of type 'UIActivityIndicatorView' has no member 'scale'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment