使UINavigationBar透明化 [英] Make UINavigationBar transparent

查看:105
本文介绍了使UINavigationBar透明化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使 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






讨论



由于半透明设置为 YES 可以解决问题https://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationBar_Class/Reference/UINavigationBar.html#//apple_ref/occ/instp/UINavigationBar/translucent\"rel =noreferrer> 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆