Skip to content

Instantly share code, notes, and snippets.

@programmarchy
Created November 15, 2018 06:50
Show Gist options
  • Save programmarchy/642d01cd7a10417349b369f2148a765f to your computer and use it in GitHub Desktop.
Save programmarchy/642d01cd7a10417349b369f2148a765f to your computer and use it in GitHub Desktop.
A simple drop in controller for making a UIView look busy
import UIKit
class BusyController<V: UIView> {
let view: V
var busyView: UIView? = nil
var busyText: String = "Loading...".localized()
init(view: V) {
self.view = view
}
open var isBusy: Bool = false {
didSet {
if isBusy {
addBusyView()
} else {
removeBusyView()
}
}
}
open var centerXAnchor: NSLayoutXAxisAnchor {
return view.centerXAnchor
}
open var centerYAnchor: NSLayoutYAxisAnchor {
return view.centerYAnchor
}
open func addBusyView() {
if let _ = self.busyView {
return
}
let busyView = UIView()
busyView.translatesAutoresizingMaskIntoConstraints = false
let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
activityIndicatorView.isHidden = false
activityIndicatorView.startAnimating()
busyView.addSubview(activityIndicatorView)
let textLabel = UILabel()
textLabel.translatesAutoresizingMaskIntoConstraints = false
textLabel.text = busyText
busyView.addSubview(textLabel)
view.addSubview(busyView)
NSLayoutConstraint.activate([
NSLayoutConstraint(item: busyView, attribute: .leading, relatedBy: .equal, toItem: activityIndicatorView, attribute: .leading, multiplier: 1, constant: 0),
NSLayoutConstraint(item: textLabel, attribute: .leading, relatedBy: .equal, toItem: activityIndicatorView, attribute: .trailing, multiplier: 1, constant: 5),
NSLayoutConstraint(item: textLabel, attribute: .trailing, relatedBy: .equal, toItem: busyView, attribute: .trailing, multiplier: 1, constant: 0),
NSLayoutConstraint(item: textLabel, attribute: .top, relatedBy: .equal, toItem: busyView, attribute: .top, multiplier: 1, constant: 0),
NSLayoutConstraint(item: textLabel, attribute: .bottom, relatedBy: .equal, toItem: busyView, attribute: .bottom, multiplier: 1, constant: 0),
NSLayoutConstraint(item: activityIndicatorView, attribute: .centerY, relatedBy: .equal, toItem: textLabel, attribute: .centerY, multiplier: 1, constant: 0)
])
centerYAnchor.constraint(equalTo: busyView.centerYAnchor).isActive = true
centerXAnchor.constraint(equalTo: busyView.centerXAnchor).isActive = true
self.busyView = busyView
}
open func removeBusyView() {
busyView?.removeFromSuperview()
busyView = nil
}
}
class BusyTableController: BusyController<UITableView> {
override var centerXAnchor: NSLayoutXAxisAnchor {
return view.frameLayoutGuide.centerXAnchor
}
override var centerYAnchor: NSLayoutYAxisAnchor {
return view.frameLayoutGuide.centerYAnchor
}
override func addBusyView() {
super.addBusyView()
view.separatorStyle = .none
view.setNeedsLayout()
}
override func removeBusyView() {
super.removeBusyView()
view.separatorStyle = .singleLine
view.setNeedsLayout()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment