简体   繁体   English

如何使UINavigationBar背景透明?

[英]How to make UINavigationBar background transparent?

First of all, I've seen all the answers at How to make UINavigationBar Transparent in IOS 8?首先,我已经在How to make UINavigationBar Transparent in IOS 8? 中看到了所有答案。 Transparent UINavigationBar and Make UINavigationBar transparent . Transparent UINavigationBarMake UINavigationBar transparent

They just don't seem to work for me.它们似乎对我不起作用。

My regular view controller (before trying to make the navigation bar transparent) doesn't have any issues:我的常规视图控制器(在尝试使导航栏透明之前)没有任何问题:

在此处输入图像描述

I'm using (tried both in viewDidLoad and viewWillAppear: ):我正在使用(在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];

I'm getting this:我明白了:

在此处输入图像描述

Gray status bar background, completely white navigation bar which doesn't blend with the status bar, and then the view starts.灰色状态栏背景,完全白色的导航栏不与状态栏融合,然后视图开始。 All the 'solutions' at the other questions' answers' yield the same result for me.其他问题答案中的所有“解决方案”对我来说都产生了相同的结果。

I've also tried setting self.edgesForExtendedLayout = UIRectEdgeNone;我也试过设置self.edgesForExtendedLayout = UIRectEdgeNone; or self.edgesForExtendedLayout = UIRectEdgeAll;self.edgesForExtendedLayout = UIRectEdgeAll; but that also didn't have any impact.但这也没有任何影响。

How can I make my navigation bar transparent without messing up everything?我怎样才能让我的导航栏透明而不弄乱一切?

UPDATE: Following Warif Akhand Rishi's answer, I've changed self.navigationController.view.backgroundColor = [UIColor clearColor];更新:根据 Warif Akhand Rishi 的回答,我更改了self.navigationController.view.backgroundColor = [UIColor clearColor]; to self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]; self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]; , now I'm getting a gray, unified status/navbar, but still not transparent: ,现在我得到一个灰色的、统一的状态/导航栏,但仍然不透明:

在此处输入图像描述

UPDATE 2: I've hooked up the view debugger, and that gray background seems to come from deep down from the roots of view hierarchy, and my view's content is not extending up.更新 2:我连接了视图调试器,灰色背景似乎来自视图层次结构的根部,我的视图内容没有向上扩展。 I've tried self.edgesForExtendedLayout = UIRectEdgeAll;我试过self.edgesForExtendedLayout = UIRectEdgeAll; again with the latest code but still no avail:再次使用最新代码但仍然无济于事:

在此处输入图像描述

swift 4 transparent nav bar: (be sure view extends behind nav bar to show through, otherwise will just be black) swift 4 透明导航栏:(确保视图延伸到导航栏后面以显示出来,否则只会是黑色)

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

or just match navbar color to color of your current vc, but keep it opaque.或者只是将导航栏颜色与当前 vc 的颜色匹配,但保持不透明。 with translucent set to false child views will line up with navbar instead of going under it.将半透明设置为 false 的子视图将与导航栏对齐而不是在其下方。

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

Change your改变你的

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

to this对此

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

Okay, after struggling, I've solved the problem on my own.好吧,经过一番挣扎,我已经自己解决了这个问题。 There was more than one problem.问题不止一个。 It wasn't about the extended edges, it was about the line self.navigationController.view.backgroundColor = [UIColor clearColor];这不是关于延伸的边缘,而是关于线self.navigationController.view.backgroundColor = [UIColor clearColor]; (which had to be self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]; as Warif Akhand Rishi suggested) and also my table view's clip subviews property. (必须是self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];正如 Warif Akhand Rishi 所建议的)以及我的表视图的剪辑子视图属性。 I've changed that line and also turned off clipping of my table view and now it works as expected.我已经更改了那条线并关闭了我的表格视图的剪裁,现在它按预期工作了。

For iOS 13 and the UINavigationBarAppearance API:对于 iOS 13 和 UINavigationBarAppearance API:

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

Eliminate 5+ lines of shadow/background/color code!消除 5 行以上的阴影/背景/颜色代码!

I'm a little late to the party, but I recently needed to do the same thing and I found the following actually works best (because it removes all shadows and bleed-throughs you may have from something lower in the stack):我来晚了一点,但我最近需要做同样的事情,我发现下面的方法效果最好(因为它消除了你可能从堆栈中较低的东西中获得的所有阴影和渗透):

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

1.Your NavigationBar is white,not black.So you must have a view (a white view) under NavigationBar,which is the superview of your greyView.The transparent setting works,but you cann't see it,because the fontcolor is white too. 1.你的NavigationBar是白色的,不是黑色的。所以你必须在NavigationBar下有一个视图(白色视图),它是你的greyView的父视图。透明设置有效,但你看不到它,因为字体颜色是白色也。
2.So you have to update your greyView's constraints,so it can extends under navigationbar.And then you can see your white title. 2.所以你必须更新你的greyView的约束,所以它可以在导航栏下扩展。然后你可以看到你的白色标题。
3.Maybe you have to Change your statusBar's UIStatusBarStyle to default or lightcontent,I noticed the font color of statusBar is white too. 3.也许你必须将statusBar的UIStatusBarStyle更改为default或lightcontent,我注意到statusBar的字体颜色也是白色的。

The below code works for me下面的代码对我有用

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

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

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