如何在 iOS 7 中使导航栏透明?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20319439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make navigation bar transparent in iOS 7?
提问by user1244109
Is there a way to make the bar of a navigation controller totally transparent?
有没有办法让导航控制器的栏完全透明?
What i've tried:
我试过的:
[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor clearColor]];
[self.navigationController.navigationBar setAlpha:0.0];
but the bar's appearance does not change, and stays white translucent. I'm trying to get visible the bar items, but not the bar itself. Can someone point me in the right direction? Thanks
但酒吧的外观没有改变,并保持白色半透明。我试图让酒吧项目可见,但不是酒吧本身。有人可以指出我正确的方向吗?谢谢
回答by Pradhyuman sinh
If anybody is wondering how to achieve this in iOS 7, here's a solution (iOS 6 compatible too)
如果有人想知道如何在 iOS 7 中实现这一点,这里有一个解决方案(iOS 6 也兼容)
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
Setting translucent to YES on the navigation bar does the trick, due to a behaviour discussed in the UINavigationBar documentation. I'll report here the relevant fragment:
由于 UINavigationBar 文档中讨论的行为,在导航栏上将 translucent 设置为 YES 可以解决问题。我将在这里报告相关片段:
If you set this property to YES on a navigation bar with an opaque custom background image, the navigation bar will apply a system opacity less than 1.0 to the image.
如果在带有不透明自定义背景图像的导航栏上将此属性设置为 YES,导航栏将对图像应用小于 1.0 的系统不透明度。
Reference from: Make UINavigationBar transparent
回答by Haresh Ghatala
You can make Navigation Bar Transparent with following code
您可以使用以下代码使导航栏透明
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setShadowImage:[UIImage new]];
[self.navigationBar setTranslucent:YES];