Empty backgroundView
Displaying a background view for when your datasource is empty is intuitive. Define the view you would like to display and the rest is taken care of
dataSource.emptyDataSourceView = createEmptybackgroundView()
func createEmptybackgroundView() -> UIView {
let emptyContainerView = UIView()
let emptyViewLabel = UILabel()
emptyViewLabel.translatesAutoresizingMaskIntoConstraints = false
emptyViewLabel.text = "Still loading data... :)"
emptyViewLabel.font = UIFont.boldSystemFont(ofSize: 25)
emptyViewLabel.numberOfLines = 0
emptyViewLabel.textAlignment = .center
let activityView = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)
activityView.translatesAutoresizingMaskIntoConstraints = false
activityView.hidesWhenStopped = true
emptyContainerView.addSubview(emptyViewLabel)
emptyContainerView.addSubview(activityView)
let paddingConstant: CGFloat = 12.0
emptyViewLabel.leadingAnchor.constraint(equalTo: emptyContainerView.leadingAnchor, constant: paddingConstant).isActive = true
emptyViewLabel.trailingAnchor.constraint(equalTo: emptyContainerView.trailingAnchor, constant: -paddingConstant).isActive = true
emptyViewLabel.centerYAnchor.constraint(equalTo: emptyContainerView.centerYAnchor).isActive = true
emptyViewLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
activityView.topAnchor.constraint(equalTo: emptyViewLabel.bottomAnchor, constant: paddingConstant).isActive = true
activityView.centerXAnchor.constraint(equalTo: emptyContainerView.centerXAnchor).isActive = true
activityView.startAnimating()
return emptyContainerView
}
