使UINavigationBar透明化 [英] Make UINavigationBar transparent
问题描述
如何使 UINavigationBar透明?虽然我希望它的条形项目保持可见。
How do you make a UINavigationBar transparent? Though I want its bar items to remain visible.
推荐答案
如果有人想知道如何在iOS 7+中实现这一点,这里有一个解决方案(iOS 6兼容)
If anybody is wondering how to achieve this in iOS 7+, here's a solution (iOS 6 compatible too)
在Objective-C中
In Objective-C
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
在swift 3(iOS 10)中
In swift 3 (iOS 10)
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
在swift 2中
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true
讨论
Discussion
Setting translucent
to YES
on the navigation bar does the trick, due to a behavior discussed in the UINavigationBar
documentation. I'll report here the relevant fragment:
如果将此属性设置为
YES
在具有不透明自定义背景图像的导航栏上,导航栏将对图像应用小于1.0的系统不透明度。
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.
这篇关于使UINavigationBar透明化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!