Created
April 24, 2019 10:44
-
-
Save nguyentruongky/0e9a3ec53fe5e5fb5f9ba64328b2db79 to your computer and use it in GitHub Desktop.
Add loading indicator with color, size to any views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Value of type 'UIActivityIndicatorView' has no member 'scale'