iOS 15 导航栏(Navigation)变白(导航栏不见)

iOS 15 导航栏(Navigation)变白(导航栏不见),第1张

目录 前言解决方法没有使用大标题使用了大标题 项目

前言

环境

Xcode 13.0  
iOS 15.0  

源码放在了github,想看可以直接翻到文章最下面

解决方法

主要是以下两个属性(UINavigationController的属性)

// 静止样式
self.navigationBar.standardAppearance;
// 滚动样式
self.navigationBar.scrollEdgeAppearance;

下面只列了 UINavigationController 主要处理代码,想看全部可去文章底部下载源码

没有使用大标题

import UIKit

/// 不使用大标题
class TestOneNC: UINavigationController {
    
    override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        print(#function);
        self.xq_init()
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        print(#function);
        self.xq_init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        print(#function);
        self.xq_init()
    }
    
    func xq_init() {
        print(#function);
        self.navigationBar.prefersLargeTitles = false
        self.navigationBar.barTintColor = .red;
        self.navigationBar.tintColor = .green
        
        let appearance = UINavigationBarAppearance.init()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UIColor.red
        appearance.shadowImage = UIImage.init()
        appearance.shadowColor = UIColor.clear
        self.navigationBar.standardAppearance = appearance
        self.navigationBar.scrollEdgeAppearance = appearance
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print(#function);
    }
    
}
使用了大标题

import UIKit

/// 使用大标题
class TestNC: UINavigationController {
    
    var appearance_test: UINavigationBarAppearance?
    
    override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        print(#function);
        self.xq_init()
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        print(#function);
        self.xq_init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        print(#function);
        self.xq_init()
    }
    
    func xq_init() {
        print(#function);
        self.navigationBar.prefersLargeTitles = true
        self.navigationBar.barTintColor = .red;
        self.navigationBar.isTranslucent = true
        self.navigationBar.tintColor = .green
        
        let appearance = UINavigationBarAppearance.init()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UIColor.red
        appearance.shadowImage = UIImage.init()
        appearance.shadowColor = UIColor.clear
        self.appearance_test = appearance
        // wxq 静止样式
        self.navigationBar.standardAppearance = appearance
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print(#function);
    }
    
    func xq_scrollEdgeAppearance(_ appearance: UINavigationBarAppearance?) {
        // wxq 滚动样式
        self.navigationBar.scrollEdgeAppearance = appearance
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        print(#function, self.viewControllers);
        if self.viewControllers.count >= 1 {
            viewController.hidesBottomBarWhenPushed = true;
            viewController.navigationItem.largeTitleDisplayMode = .never;
        }
        
        // wxq 第一次设置即可
        if self.viewControllers.count == 1 {
            self.xq_scrollEdgeAppearance(self.appearance_test)
        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    override func popToRootViewController(animated: Bool) -> [UIViewController]? {
        print(#function, self.viewControllers);
        self.xq_scrollEdgeAppearance(nil)
        return super.popToRootViewController(animated: animated)
    }
    
    override func popViewController(animated: Bool) -> UIViewController? {
        print(#function, self.viewControllers);
        if self.viewControllers.count <= 2 {
            self.xq_scrollEdgeAppearance(nil)
        }
        return super.popViewController(animated: animated)
    }

}

项目

XQNavigationBar15BlankDemo项目地址

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/996679.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存