简体   繁体   English

由于奇怪的 UINavigationBar 视图层次结构,自定义 UINavigationBar 不起作用

[英]Customizing UINavigationBar not working because of weird UINavigationBar view hierarchy

My app has many ViewControllers that are pushed on navigation stack.我的应用程序有许多推送到导航堆栈上的 ViewController。

I have configured UINavigationBar appearance globally in AppDelegate as below.我已经在 AppDelegate 中全局配置了 UINavigationBar 外观,如下所示。

let appearance = UINavigationBar.appearance()
appearance.barTintColor = myColor
appearance.tintColor = .white
appearance.isTranslucent = false
let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,
                      NSAttributedString.Key.font: myFont, size: mySize)]
appearance.titleTextAttributes = textAttributes as [NSAttributedString.Key : Any]

All ViewControllers work as expected except only one ViewController.所有 ViewController 都按预期工作,只有一个 ViewController 除外。

Below is one of the VCs that works as expected.下面是按预期工作的 VC 之一。 It shows color and font that I want.它显示我想要的颜色和字体。

在此处输入图像描述

And below is the ViewController that shows different look unlike others.下面是显示不同外观的 ViewController。

在此处输入图像描述

I can't understand the reason why only one navigationBar on this VC shows different appearance.我不明白为什么这个 VC 上只有一个导航栏显示不同的外观。

So I've done debugging view hierarchy.所以我已经完成了调试视图层次结构。

Below is the view hierarchy of VCs that works as expected.下面是按预期工作的 VC 的视图层次结构。

在此处输入图像描述

And below is the view hierarchy of VC that shows weird look.下面是显示怪异外观的 VC 视图层次结构。

在此处输入图像描述

As seen on the picture, the problematic NavigationBar has two more layers, UIVisualEffectView and UIVisualEffectBackdropView.如图所示,有问题的 NavigationBar 还有两层,UIVisualEffectView 和 UIVisualEffectBackdropView。

I am an experienced iOS developer and have no idea why this happens.我是一位经验丰富的 iOS 开发人员,不知道为什么会这样。

I carefully checked all the setting related to NavigationBar on the IB but found no difference from others.我仔细检查了IB上所有与NavigationBar相关的设置,没有发现和其他的有什么区别。

I even removed the ViewController, embedding NavigationController completely and rebuilt them from scratch without luck.我什至删除了 ViewController,完全嵌入了 NavigationController 并从头开始重建它们,但运气不佳。

Please somebody explain me why only this NavigationBar has different structure.请有人解释为什么只有这个 NavigationBar 有不同的结构。

I'm working on iOS 13.3 & Xcode 11.3.1我正在研究 iOS 13.3 & Xcode 11.3.1

Working here on iOS15, I tried a lot of things to customize the nav bar appearance to achieve a simple opaque color, nothing worked except this:在 iOS15 上工作,我尝试了很多方法来自定义导航栏外观以实现简单的不透明颜色,除了这个之外没有任何效果:

In viewDidLoad在viewDidLoad中

if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
         appearance.backgroundColor = [UIColor orangeColor];
        self.navigationController.navigationBar.standardAppearance = appearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
}

Swift version: Swift版本:

let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .orange
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance

Finally if you only want to apply this on the current controller, and restore the default nav bar just do the same but with a fresh UINavigationBarAppearance() on viewWillDisappear.最后,如果您只想在当前 controller 上应用它,并恢复默认导航栏,只需执行相同操作,但在 viewWillDisappear 上使用新的 UINavigationBarAppearance()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM