Empty
当您没有数据要显示时,快速简单的UIView使用。也非常适合显示错误信息!
什么是Empty?
在那些没有数据向用户显示的时候,Emergency行动起来!Emergency是一种快速简单的方式向用户显示一个消息并通过添加一行代码的按钮来帮助执行下一步。
通过默认设置配置,适用于大部分使用情况,但足够可自定义,以涵盖更多。
为什么使用Empty?
- Swift API
- 轻量级。无依赖。
- UI测试友好。
- 使用默认值设置,这应该适用于95%的使用情况。但对于其他情况也有可定制性。
- 完整的测试套件。
- 完整的文档.
我建议您查看其他两个与Empty协同工作的库: PleaseHold 和 Swapper.
安装
Empty可以通过CocoaPods获取。要安装,只需将以下行添加到你的Podfile中
pod 'Empty', '~> version-here'
将version-here
替换为:,因为这是当前最新的版本。
入门指南
- 在你的
UIViewController
中创建一个EmptyView
实例。你可以通过Storyboard在Storyboard中添加一个UIView
并设置其类为EmptyView
来实现,或者在你的 Swift 代码中创建实例
let emptyView: EmptyView = {
let view = EmptyView()
view.title = nil // title is optional
view.message = nil // message is optional
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
- 将
EmptyView
添加到你的UIViewController
。很简单。
如果你想更改标题或信息UILabel
的文本,你可以在运行时更改它们
emptyView.title = "New title"
emptyView.message = "New message"
或者,如果你想隐藏其中一个标签,只需将其设置为nil
emptyView.title = nil
emptyView.message = nil
- 如果你想向
EmptyView
添加1+个按钮
class ViewController: UIViewController {
enum EmptyButtons: String {
case retry
case dontRetry
}
emptyView.delegate = self
emptyView.addButton(id: EmptyButtons.retry.rawValue, message: "Retry")
emptyView.addButton(id: EmptyButtons.dontRetry.rawValue, message: "Dont Retry")
}
extension ViewController: EmptyViewDelegate {
func buttonPressed(id: String) {
guard let buttonPressed = EmptyButtons(rawValue: id) else {
return
}
let alert: UIAlertController
switch buttonPressed {
case .retry:
// Handle retry
case .dontRetry:
// handle don't retry
}
}
}
配置Empty
Empty无需配置即可正常工作。然而,如果你希望自定义它,你可以。
如果你想更改应用程序中所有Empty
实例的默认值,请更改默认单例中的值
Empty.defaultConfig.viewPadding = 10.0
或者,你可以配置一个单一的EmptyView
实例
let emptyView = EmptyView()
emptyView.config.newButton = {
let button = EmptyViewConfig.defaultButton
button.setTitleColor(.blue, for: .normal)
return button
}
pleaseHoldView.addButton(id: "id", message: "Retry") // Add a button here, it will have blue text color!
为了将配置重用于EmptyView
,建议创建一个EmptyViewConfigPresent
/// Convenient set of `UIView`s that are dark in color. Great for light colored backgrounds.
public struct DarkEmptyViewConfig: EmptyViewConfigPreset {
public var titleLabel: UILabel {
let label = EmptyViewConfig.defaultTitleLabel
label.textColor = .darkText
return label
}
public var messageLabel: UILabel {
let label = EmptyViewConfig.defaultMessageLabel
label.textColor = .darkText
return label
}
public var button: UIButton {
let label = EmptyViewConfig.defaultButton
label.setTitleColor(.darkGray, for: .normal)
return label
}
}
let emptyView = EmptyView()
emptyView.config = DarkEmptyViewConfig().config
为了方便起见,Empty
提供了一个浅色和深色主题的并发现:DarkEmptyViewConfig
和LightEmptyViewConfig
,你可以使用或者扩展它们。
你可以为Empty
配置很多内容。有关所有配置选项的列表,请参阅EmptyViewConfig文档
示例
Empty附带了您可以使用来探索库的示例应用。要运行示例项目,请克隆代码库,从示例目录中运行pod install
。然后,在XCode中打开工作区。
开发
Empty是一个非常简单的CocoaPods库项目。按照以下说明进行操作可以获得最佳开发体验。
- 安装cocoapods/gems并设置工作区
$> bundle install
$> cd Empty/Example
$> pod install
- 通过overcommit设置git钩子,使用git时将为你运行一些杂项任务。
$> overcommit --install
作者
贡献
Empty欢迎拉取请求。查看问题列表,了解我计划工作的任务。如果您希望通过这种方式做出贡献,请检查它们。
想要向Empty添加功能吗?在决定花大量时间向库添加功能之前,请先创建一个问题,说明您希望添加什么。这可能会在您的目的不适合Empty使用案例的情况下节省您的时间。
许可协议
Empty遵循MIT许可协议。有关更多信息,请参阅LICENSE文件。