SwiftyAppearance
UIAppearance
允许在单个集中位置配置全局应用程序的外观感觉,而不是将设计点滴散布在各个类、xibs 和 storyboard 中。
SwiftyAppearance 为此方法增添了一点风格。
示例
要运行示例项目,克隆仓库并运行 SwiftAppearanceDemo
应用
简要说明
为了通过 UIAppearance
获得最佳效果,请避免直接从 UIKit 基础类继承,而应插入自定义基础类。考虑类层次结构如下:UIViewController
→ AppViewController
→ UserListViewController
。这样做可以在整个应用程序中应用外观,同时保持第三方屏幕(如短信或邮件编写器)完整无误。
此外,创建一个用于替代 UIView
的自定义“背景视图”类作为大多数视图控制器根视图也很有用。这允许全局自定义视图控制器背景,而不会影响其他视图。
演示应用程序定义以下根类用于全局样式
AppViewController
AppTabBarController
AppNavigationController
AppBackgroundView
appearance(inAny: [AppViewController.self, AppTabBarController.self, AppNavigationController.self]) {
UITabBar.appearance {
$0.barStyle = .black
$0.barTintColor = UIColor(rgb: 0x2c3e50)
$0.tintColor = UIColor(rgb: 0xecf0f1)
}
}
此外,我们还定义了两个用于不同屏幕的导航控制器子类
FirstNavigationController
SecondNavigationController
FirstNavigationController.appearance {
UINavigationBar.appearance {
$0.barStyle = .black
$0.barTintColor = UIColor(rgb: 0x2980b9)
$0.tintColor = UIColor(rgb: 0xecf0f1)
}
}
SecondNavigationController.appearance {
UINavigationBar.appearance {
$0.barStyle = .`default`
$0.barTintColor = UIColor(rgb: 0xf39c12)
$0.titleTextAttributes = [NSForegroundColorAttributeName: UIColor(rgb: 0xc0392b)]
}
}
最后,我们自定义了我们的视图控制器
FirstViewController.appearance {
AppBackgroundView.appearance {
$0.backgroundColor = UIColor(rgb: 0xecf0f1)
$0.tintColor = UIColor(rgb: 0x34495e)
}
UILabel.appearance {
$0.textColor = UIColor(rgb: 0x2c3e50)
}
}
SecondViewController.appearance {
AppBackgroundView.appearance {
$0.backgroundColor = UIColor(rgb: 0xf1c40f)
}
UILabel.appearance {
$0.textColor = UIColor(rgb: 0xe67e22)
}
}
API概述
SwiftyAppearance定义了一系列函数来处理嵌套外观作用域。每个作用域定义了更具体的案例——要么通过添加嵌套容器,要么通过添加一些特性。
自由函数和UIAppearanceContainer扩展仅引入嵌套作用域,而UIAppearance扩展还提供了配置其属性的代理对象。
自由函数
func appearance(for traitCollection: UITraitCollection? = nil, in containerTypes: [UIAppearanceContainer.Type] = [], _ block: () -> Void)
— 将集合中的特性和容器添加到作用域func appearance(for traitCollection: UITraitCollection? = nil, inAny containerTypes: [UIAppearanceContainer.Type], _ block: () -> Void)
— 将特质添加到作用域,并为每个提供的容器定义一组嵌套作用域
UIAppearance和UIAppearanceContainer扩展
static func UIAppearanceContainer.appearance(style: AppearanceStyle = nil, for traitCollection: UITraitCollection? = nil, _ block: () -> Void)
— 将嵌套容器添加到作用域,可选地指定样式static func UIAppearance.appearance(style: AppearanceStyle = nil, for traitCollection: UITraitCollection? = nil, _ block: (_ proxy: Self) -> Void)
— 将集合中的特性添加到作用域,提供具有可选样式类的外观代理,然后如果适用将当前类作为容器添加
UIWindow和UIApplication扩展
func UIWindow.refreshAppearance(animated: Bool)
— 刷新窗口的外观func UIApplication.refreshAppearance(animated: Bool)
— 刷新应用程序中所有窗口的外观
UIView和UIViewController扩展
@IBInspectable public var appearanceStyleName: String
—— 从Interface Builder设置外观样式的可检查属性var appearanceStyle: AppearanceStyle
—— 外观样式访问器func setAppearanceStyle(_ style: AppearanceStyle, animated: Bool)
—— 选择性地动画更新外观样式
安装
CocoaPods
要使用CocoaPods将SwiftyAppearance集成到您的Xcode项目中,请在您的Podfile中添加以下行:
pod "SwiftyAppearance"
Carthage
要使用Carthage将SwiftyAppearance集成到您的Xcode项目中,在您的Cartfile中指定它:
github "victor-pavlychko/SwiftyAppearance"
仅复制文件
SwiftyAppearance由两个文件组成,没有外部依赖项。您只需将文件复制到项目中即可。
作者
维克托·帕夫利奇科,[email protected]
授权
SwiftyAppearance 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。