如何使UINavigationBar背景透明化?

时间:2016-01-10 15:30:02

标签: ios uinavigationcontroller uinavigationbar

首先, 我在How to make UINavigationBar Transparent in IOS 8? Transparent UINavigationBarMake UINavigationBar transparent看到了所有答案。

他们似乎并不适合我。

我的常规视图控制器(在尝试使导航栏透明之前)没有任何问题:

enter image description here

我正在使用(在viewDidLoadviewWillAppear:中尝试过):

[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                              forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];

我得到了这个:

enter image description here

灰色状态栏背景,完全白色的导航栏,它不会与状态栏混合,然后视图开始。所有的解决方案'在其他问题'答案'对我产生相同的结果。

我也尝试过设置self.edgesForExtendedLayout = UIRectEdgeNone;self.edgesForExtendedLayout = UIRectEdgeAll;,但这也没有产生任何影响。

如何在不弄乱所有内容的情况下使导航栏变得透明?

更新:在Warif Akhand Rishi的回答之后,我已将self.navigationController.view.backgroundColor = [UIColor clearColor];更改为self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];,现在我变得灰暗,统一状态/导航栏,但仍然不透明:

enter image description here

更新2:我已经连接了视图调试器,并且灰色背景似乎来自视图层次结构的根本深处,我的视图内容是没有延伸。我已经使用最新的代码再次尝试self.edgesForExtendedLayout = UIRectEdgeAll;,但仍无济于事:

enter image description here

7 个答案:

答案 0 :(得分:15)

swift 4透明导航栏: (确保视图延伸到导航栏后面显示,否则将只是黑色)

navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage() //remove pesky 1 pixel line

或者只是将导航栏颜色与当前vc的颜色匹配,但保持不透明。将半透明设置为false子视图将与导航栏对齐,而不是在其下面。

navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.barTintColor = UIColor.yourColor
navigationController?.navigationBar.shadowImage = UIImage() //remove pesky 1 pixel line

答案 1 :(得分:7)

更改您的

self.navigationController.view.backgroundColor = [UIColor clearColor];

到这个

self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];

答案 2 :(得分:2)

好吧,经过挣扎,我自己解决了这个问题。有一个以上的问题。它不是关于扩展边缘,而是关于线self.navigationController.view.backgroundColor = [UIColor clearColor];(必须是Warif Akhand Rishi建议的self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];)以及我的表格视图的剪辑子视图属性。我已经更改了那一行并且还关闭了我的表视图的剪辑,现在它按预期工作。

答案 3 :(得分:0)

1.你的NavigationBar是白色的,而不是黑色。所以你必须在NavigationBar下有一个视图(白色视图),这是你的greyView的超级视图。透明设置有效,但是你看不到它,因为fontcolor也是白色的 2.因此,您必须更新您的greyView约束,以便它可以在导航栏下进行扩展。然后您可以看到您的白色标题。
3.也许你必须将你的statusBar的UIStatusBarStyle改为默认或轻量级,我注意到statusBar的字体颜色也是白色。

答案 4 :(得分:0)

以下代码对我有用

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
self.navigationController?.navigationBar.backgroundColor = .clear

答案 5 :(得分:0)

我参加聚会有点晚了,但是我最近需要做同样的事情,我发现以下方法实际上效果最好(因为它可以消除堆栈中较低位置上的所有阴影和渗出物) :

guard let navBar = navigationController?.navigationBar else { return }
navBar.barStyle = .black
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.shadowImage = UIImage()
navBar.isTranslucent = true
navBar.isHidden = false

答案 6 :(得分:0)

对于 iOS 13 和 UINavigationBarAppearance API:

let navAppearance = UINavigationBarAppearance()
navAppearance.configureWithTransparentBackground()
self.navigationItem.standardAppearance = navAppearance

消除 5 行以上的阴影/背景/颜色代码!