使iOS 8.1中的下方图像的导航栏透明

时间:2014-12-30 16:26:04

标签: ipad swift ios8.1

我尝试将导航栏设置为透明,如下图所示:

enter image description here

我在transparent navigation bar ios尝试了解决方案,但我没有得到上述结果,我只在左侧显示图标,但导航栏中没有任何颜色,完全透明。但如果我设置背景颜色,透明度就会消失。

有任何方法可以在导航栏中设置颜色并使其透明吗?

提前致谢。

4 个答案:

答案 0 :(得分:30)

刚刚在8.1模拟器上检查并得到了与你的图片非常相似的结果

    let bar:UINavigationBar! =  self.navigationController?.navigationBar

    bar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
    bar.shadowImage = UIImage()
    bar.backgroundColor = UIColor(red: 0.0, green: 0.3, blue: 0.5, alpha: 0.3)

这里的要点是带有alpha的背景颜色。

检查附图,也许我错过了什么?

enter image description here

答案 1 :(得分:12)

要全局设置此样式,请使用UIAppearance API。在AppDelegate的application:didFinishLaunchingWithOptions:中添加以下代码:

// Sets background to a blank/empty image
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
// Sets shadow (line below the bar) to a blank image
UINavigationBar.appearance().shadowImage = UIImage()
// Sets the translucent background color
UINavigationBar.appearance().backgroundColor = UIColor(red: 0.0, green: 0.3, blue: 0.5, alpha: 0.3)
// Set translucent. (Default value is already true, so this can be removed if desired.)
UINavigationBar.appearance().translucent = true

答案 2 :(得分:0)

您是否尝试过设置navigationBar的alpha属性?在导航控制器的根视图控制器中......

[self.navigationController.navigationBar setBackgroundColor:[UIColor greenColor]];
[self.navigationController.navigationBar setAlpha:0.3f];

答案 3 :(得分:0)

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

如果上面的代码不起作用,则将edgeForExtendedLayout设置为all。

self.edgesForExtendedLayout = .all