这是用Swift编写的iOS UI控件,它在现有视图上显示“无内容”消息。当您想要在现有视图上显示缺失内容消息并建议一个动作时,这可能很有用。例如:“购物车为空”、“没有新消息”、“没有收藏”、“没有评论”等。
例如,假设我们正在构建一个购物应用的购物车屏幕。当购物车为空时,我们可以显示“购物车为空”消息和“搜索”按钮。
以下是使用Glyptodon在你的现有视图中显示“购物车为空”消息的方法
view.glyptodon.show("Cart is empty", withButton: "Search") {
// User has tapped the "Search" button.
}
view.glyptodon.show
方法会将一个子视图添加到你的现有视图中。view.glyptodon.hide()
来移除glyptodon视图。有三种方法可以将Glyptodon添加到你的项目中。
添加源代码(iOS 7+)
只需将GlyptodonDistrib.swift文件添加到Xcode项目中。
使用Carthage设置(iOS 8+)
将github "marketplacer/Glyptodon" ~> 2.0
添加到你的Cartfile中,然后运行carthage update
。
使用CocoaPods设置(iOS 8+)
如果你在使用CocoaPods,请将此文本添加到你的Podfile中,然后运行pod install
。
use_frameworks!
target 'Your target name'
pod 'Glyptodon', '~> 2.0'
如果你使用了Carthage或CocoaPods设置方法,请将import Glyptodon
添加到你的源代码中。
Glyptodon是UIView类的一个扩展。你可以通过任何UIView或其子类的实例使用glyptodon
属性来访问它。例如,这是你的视图控制器的view
属性。
// Show message
view.glyptodon.show("No new messages")
// Hide message
view.glyptodon.hide()
// Show message with a button
view.glyptodon.show("Cart is empty", withButton: "Go shopping") {
// Do something when the button is tapped.
}
在显示视图之前,将glyptodon.style
属性设置以样式化视图。有关完整的配置选项列表,请参阅样式手册。
// Set the view background color
view.glyptodon.style.view.backgroundColor = GlyptodonColor.fromHexString("#EEEEEE")
// Set the title font
view.glyptodon.style.title.font = UIFont.preferredFontForTextStyle(UIFontTextStyleTitle1)
// Set the title color
view.glyptodon.style.title.color = GlyptodonColor.fromHexString("#666666")
// Set the button font
view.glyptodon.style.button.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
// Set the button color
view.glyptodon.style.button.color = GlyptodonColor.fromHexString("#007AFF")
// Set the color of the tapped button
view.glyptodon.style.button.colorHighlighted = GlyptodonColor.fromHexString("#007AFF33")
有时在单元测试中验证消息视图及其内容的存在是有用的。该库提供了以下辅助属性,以简化单元测试。
// Indicates if the view is currently visible
view.glyptodon.visible
// Returns the currently displayed title label
view.glyptodon.titleLabel
// Returns the currently displayed button
view.glyptodon.button
该项目包含一个演示iOS应用。
Glyptodon库是在MIT许可证下发布的。