AppearanceNavigationController 使用教程
项目介绍
AppearanceNavigationController
是一个开源项目,旨在提供一种声明式的导航栏外观配置方法。该项目通过自定义 UINavigationController
的外观,使得开发者能够更方便地管理和调整导航栏的颜色、背景等属性。
项目快速启动
安装
首先,将项目克隆到本地:
git clone https://github.com/Yalantis/AppearanceNavigationController.git
集成
将 AppearanceNavigationController
文件夹添加到你的 Xcode 项目中。
使用
在你的 UIViewController
中实现 NavigationControllerAppearanceContext
协议,并配置导航栏外观:
import UIKit
class ContentViewController: UIViewController, NavigationControllerAppearanceContext {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
navigationItem.rightBarButtonItem = editButtonItem
}
var appearance: Appearance {
didSet {
setNeedsUpdateNavigationControllerAppearance()
}
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
setNeedsUpdateNavigationControllerAppearance()
}
func prefersBarHidden(for navigationController: UINavigationController) -> Bool {
return isEditing
}
func prefersToolbarHidden(for navigationController: UINavigationController) -> Bool {
return isEditing
}
func preferredAppearance(for navigationController: UINavigationController) -> Appearance {
return isEditing ? appearance.inverse() : appearance
}
}
在 AppearanceNavigationController
中更新外观:
class AppearanceNavigationController: UINavigationController, UINavigationControllerDelegate {
public required init(coder decoder: NSCoder) {
super.init(coder: decoder)!
delegate = self
}
func updateAppearance(for viewController: UIViewController) {
if let context = viewController as? NavigationControllerAppearanceContext,
viewController == topViewController,
transitionCoordinator == nil {
setNavigationBarHidden(context.prefersBarHidden(for: self), animated: true)
setToolbarHidden(context.prefersToolbarHidden(for: self), animated: true)
applyAppearance(appearance: context.preferredAppearance(for: self), animated: true)
}
}
public func updateAppearance() {
if let top = topViewController {
updateAppearance(for: top)
}
}
}
应用案例和最佳实践
案例一:动态改变导航栏颜色
在用户编辑模式下,动态改变导航栏的颜色:
class EditViewController: UIViewController, NavigationControllerAppearanceContext {
var appearance: Appearance = Appearance()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
appearance.navigationBar.backgroundColor = .blue
setNeedsUpdateNavigationControllerAppearance()
}
func preferredAppearance(for navigationController: UINavigationController) -> Appearance {
return appearance
}
}
案例二:隐藏导航栏
在特定视图控制器中隐藏导航栏:
class FullScreenViewController: UIViewController, NavigationControllerAppearanceContext {
func prefersBarHidden(for navigationController: UINavigationController) -> Bool {
return true
}
}
典型生态项目
项目一:CustomTabBarController
结合自定义标签栏控制器,实现更复杂的界面管理:
class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if let navigationController = viewController as? AppearanceNavigationController {
navigationController.updateAppearance()
}
}
}
项目二:ThemeManager
实现主题管理器,动态切换应用主题:
class ThemeManager {
static let shared = ThemeManager()
var currentTheme: Appearance = .lightAppearance {
didSet {
NotificationCenter