使 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
讨论
在导航栏上将 translucent
设置为 YES
可以解决问题,这是由于 UINavigationBar
文档.我将在此处报告相关片段:
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屋!