Forked from nguyentruongky/view_loading_indicator.swift
Created
June 27, 2019 07:06
-
-
Save classx/a17dd316f24c243ed3beb751d503c856 to your computer and use it in GitHub Desktop.
Add loading indicator with color, size to any views
This file contains hidden or 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