AppearanceNavigationController 使用教程

AppearanceNavigationController 使用教程

AppearanceNavigationControllerExample with advanced configuration of the navigation controller's appearance项目地址:https://gitcode.com/gh_mirrors/ap/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

AppearanceNavigationControllerExample with advanced configuration of the navigation controller's appearance项目地址:https://gitcode.com/gh_mirrors/ap/AppearanceNavigationController

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶真蔷Scott

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值