iOS 中的模糊半透明导航栏

Blurred translucent navigation bar in iOS

我正在尝试在我的 Swift iOS 应用程序中获得这种模糊半透明的导航栏效果(图像中的第一个导航栏):

但我做不到。我尝试了几种方法,其中一些如下:

setBackgroundImage, shadowImage and translucent attrs in AppDelegate (Getting the NavController from the RootViewController attr)

AppDelegate main 方法中的那些行:

UINavigationBar.appearance().translucent = true;
UINavigationBar.appearance().barTintColor = UIColor(white: 1, alpha: 0.4)

从 UIBuilder 更改属性

在 viewControllers 中设置 self.automaticallyAdjustsScrollViewInsets = trueself.edgesForExtendedLayout = UIRectEdge.None

但仍然无法正常工作。我是 运行 XCode 模拟器上的应用程序,使用 iOS 8.1 和 9.0

我做错了什么?

也许 plist 更改可以解决您的问题。

第二个导航栏已经是半透明和模糊的了——它只是不如第一个导航栏那么半透明。如果您打开 Digital Color Meter 并将鼠标悬停在您的屏幕截图上,您可以看到第二个导航栏的背景在覆盖图像的区域中较暗。

第一个导航栏比第二个导航栏更半透明的原因在this question中有解释:第一个导航栏的屏幕截图是从设备上截取的运行 iOS 7.0.2,而第二个栏的屏幕截图是从设备 运行 iOS 7.0.4 中截取的。 Apple 在 iOS 和 iOS 7.1.

的这些版本之间更改了 UINavigationBar 的行为

苹果explains目前半透明机制如下:

A translucent bar mixes its barTintColor with gray before combining it with a system-defined alpha value to produce the final background color that is used to composite the bar with the content it overlies.

唯一的description模糊效果如下:

Navigation bars are translucent by default in iOS 7. Additionally, there is a system blur applied to all navigation bars.

您可以通过使用 UIImage.imageWithColor: 从具有适当 alpha 值的 UIColor 为栏制作背景图像来更改栏的透明度,但这样做会消除模糊效果。没有记录在案的方法来改变 "system-defined alpha value" 以增加栏的半透明度而不删除 "system blur".

如果您想在 iOS 8.1 和 9.0 上模仿旧的半透明和模糊效果,您需要创建自己的 UINavigationBar 子类并插入一个或多个子视图,例如带背景的 UIView使用适当的 alpha 值(用于透明度)的颜色 and/or UIVisualEffectView(用于模糊效果)。

您或许可以在 this question 的答案之一中找到组合视图。

相关:Blurring effect disappeared on iOS 7.1

didFinishLaunchingWithOptions 中添加以下代码:

对于Objective C

  [[UINavigationBar appearance] setTranslucent:NO];

为Swift3+/iOS10+

UINavigationBar.appearance().isTranslucent = false