HGPlaceholders
示例
要运行示例项目,请先克隆仓库并从 Example 目录运行 pod install
要求
- iOS 8.0+
- Xcode 9.2
你可能还喜欢
- HGCircularSlider - 一个可重复使用的自定义圆形滑块控件,用于 iOS 应用。
- HGRippleRadarView - 用于显示附近用户并具有涟漪动画的漂亮雷达视图,完全可自定义
安装
HGPlaceholders 通过 CocoaPods 可用。要安装它,只需在 Podfile 中添加以下行
pod 'HGPlaceholders'
HGPlaceholders 也可以通过 Carthage 来获取。要安装它,只需在 Cartfile 中添加以下行
github "HamzaGhazouani/HGPlaceholders"
使用
- 从 TableView 继承你的 UITableView 类或从 CollectionView 继承 UICollectionView
- 调用占位符以显示
tableView.showLoadingPlaceholder()
或collectionView.showLoadingPlaceholder()
tableView.showNoResultsPlaceholder()
或collectionView.showNoResultsPlaceholder()
tableView.showErrorPlaceholder()
或collectionView.showErrorPlaceholder()
tableView.showNoConnectionPlaceholder()
或collectionView.showNoConnectionPlaceholder()
定制
如果你只想更改图片,只需在 asset 中按照这些名称设置它们(框架首先在主包中检查)
- loading : "hg_default-loading"
- no_connection : "hg_default-no_connection"
- no_results : "hg_default-no_results"
- error : "hg_default-error"
框架包含不同的默认占位符
- 基本
tableView.placeholdersProvider = .basic
或 collectionView.placeholdersProvider = .basic
- 默认
tableView.placeholdersProvider = .default
或 collectionView.placeholdersProvider = .default
- 默认2
tableView.placeholdersProvider = .default2
或 collectionView.placeholdersProvider = .default2
- 万圣节
tableView.placeholdersProvider = .halloween
或 collectionView.placeholdersProvider = .halloween
// 乐趣 :)`
如果你想要更改项目中所有 table views 的默认占位符
class ProjectNameTableView: TableView {
override func customSetup() {
placeholdersProvider = .basic
}
}
class ProjectNameCollectionView: CollectionView {
override func customSetup() {
placeholdersProvider = .basic
}
}
你也可以添加全新的、完全可定制的占位符,请注意视图将占据 table view 的框架,占位符只能有一个操作,请查看示例项目
从头创建新主题
static var summer: PlaceholdersProvider {
var commonStyle = PlaceholderStyle()
commonStyle.backgroundColor = UIColor(red: 1.0, green: 236.0/255, blue: 209.0/255.0, alpha: 1.0)
commonStyle.actionBackgroundColor = .black
commonStyle.actionTitleColor = .white
commonStyle.titleColor = .black
commonStyle.isAnimated = false
commonStyle.titleFont = UIFont(name: "AvenirNextCondensed-HeavyItalic", size: 19)!
commonStyle.subtitleFont = UIFont(name: "AvenirNextCondensed-Italic", size: 19)!
commonStyle.actionTitleFont = UIFont(name: "AvenirNextCondensed-Heavy", size: 19)!
var loadingStyle = commonStyle
loadingStyle.actionBackgroundColor = .clear
loadingStyle.actionTitleColor = .gray
var loadingData: PlaceholderData = .loading
loadingData.image = #imageLiteral(resourceName: "summer-hat")
let loading = Placeholder(data: loadingData, style: loadingStyle, key: .loadingKey)
var errorData: PlaceholderData = .error
errorData.image = #imageLiteral(resourceName: "summer-ball")
let error = Placeholder(data: errorData, style: commonStyle, key: .errorKey)
var noResultsData: PlaceholderData = .noResults
noResultsData.image = #imageLiteral(resourceName: "summer-cocktail")
let noResults = Placeholder(data: noResultsData, style: commonStyle, key: .noResultsKey)
var noConnectionData: PlaceholderData = .noConnection
noConnectionData.image = #imageLiteral(resourceName: "summer-beach-slippers")
let noConnection = Placeholder(data: noConnectionData, style: commonStyle, key: .noConnectionKey)
let placeholdersProvider = PlaceholdersProvider(loading: loading, error: error, noResults: noResults, noConnection: noConnection)
let xibPlaceholder = Placeholder(cellIdentifier: "CustomPlaceholderCell", key: PlaceholderKey.custom(key: "XIB"))
placeholdersProvider.add(placeholders: xibPlaceholder)
return placeholdersProvider
}
向现有主题添加自定义占位符
private static var starWarsPlaceholder: Placeholder {
var starwarsStyle = PlaceholderStyle()
starwarsStyle.backgroundColor = .black
starwarsStyle.actionBackgroundColor = .clear
starwarsStyle.actionTitleColor = .white
starwarsStyle.titleColor = .white
starwarsStyle.isAnimated = false
var starwarsData = PlaceholderData()
starwarsData.title = NSLocalizedString("\"This is a new day, a\nnew beginning\"", comment: "")
starwarsData.subtitle = NSLocalizedString("Star Wars", comment: "")
starwarsData.image = #imageLiteral(resourceName: "star_wars")
starwarsData.action = NSLocalizedString("OK!", comment: "")
let placeholder = Placeholder(data: starwarsData, style: starwarsStyle, key: PlaceholderKey.custom(key: "starWars"))
return placeholder
}
let provider = PlaceholdersProvider.summer
provider.addPlaceholders(MyUtilityClass.starWarsPlaceholder)
文档
完整文档可在CocoaDocs上获得。
您还可以使用jazzy来本地安装文档。
作者
Hamza Ghazouani,[email protected]
许可
HGPlaceholders遵循MIT许可协议。更多信息请查阅LICENSE文件。