Common usage case;
TLDR;
Image you're getting data from a backend, your request succeded but it is just an empty list.
Don't left user with a blank screen, or boring alerts.
Display Empty State - Medium Blog
For better understanding, please view Example project.
To run the example project, clone the repo, and run pod install
from the Example directory first.
But here below the main concept.
Usable in any UITableViewController
, UICollectionViewController
and UIViewController
class TableViewController: UITableViewController {
lazy var emptyStateManager: EmptyStateManager = {
let esv = EmptyStateView(
messageText: "This is label belongs to empty state view that sits in UITableViewController's UITableView",
titleText: "EmptyState Title",
image: UIImage(named: "empty_state_image"),
buttonText: "Button",
centerYOffset: -100 // In case you want to move it to top, by default it is centered (offset = 0)
)
esv.buttonAction = { _ in
esv.messageText = "Button action works 👍🏻"
}
let manager = EmptyStateManager(
containerView: self.tableView,
emptyView: esv,
animationConfiguration: .init(animationType: .spring)
)
return manager
}()
var dataSource = (1...50).map { _ in UIColor.random } {
didSet {
self.tableView.reloadData()
self.emptyStateManager.reloadState()
}
}
// dataSource is the dataSource of tableView
}
Same example valid for UICollectionView contentView,
If your content view is subclass of ICollectionView/UITableView
manager knows whether it hasContent, but in case you use pure UIView as contentView
then make sure you set hasContent manually.
manager.hasContent = {
!self.dataSource.isEmpty
}
EmptyStateManager
has animationConfiguration parameter with default values.
struct AnimationConfiguration {
let animationType: AnimationType // .spring, .fromBottom, .fromLeft, .fromTop, .fromRight
let animationDuration: TimeInterval
let subItemDelayConstant: Double // image, titleLabel, messageLabel, button. Except .spring animation
let springDamping: CGFloat // .spring animation case
let initialVelocity: CGFloat
let options: UIView.AnimationOptions
}
let manager = EmptyStateManager(
containerView: ***,
emptyView: ***,
animationConfiguration: .init(animationType: .spring)
)
If you want to make your custom EmptyStateView
Just implement IEmptyStateView
protocol in your custom UIView class.
iOS 10+ Swift 5+
SwiftEmptyState is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftEmptyState'
Add new package from repository link: https://github.com/EnesKaraosman/SwiftEmptyState.git
eneskaraosman, [email protected]
SwiftEmptyState is available under the MIT license. See the LICENSE file for more info.