简体   繁体   English

导航栏的实时模糊效果-状态栏无法进入

[英]Real time blur effect for Navigation Bar - Status bar not getting in

Followed this question to get real time blur effect in Navigation Bar: 遵循此问题以在导航栏中获得实时模糊效果:

func addBlurEffect() {
var bounds = self.navigationController?.navigationBar.bounds as CGRect!
var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light)) as UIVisualEffectView
visualEffectView.frame = bounds
visualEffectView.autoresizingMask = .FlexibleHeight | .FlexibleWidth
self.navigationController?.navigationBar.addSubview(visualEffectView)
}

But the status bar remains translucent: 但是状态栏仍然是半透明的:

在此处输入图片说明

How to fix it? 如何解决?


Debugging: 调试:

    print(self.navigationController?.navigationBar.bounds)
    // Returns (0.0, 0.0, 320.0, 44.0)

    print(UIApplication.sharedApplication().statusBarFrame)
    // Returns (0.0, 0.0, 320.0, 20.0)

It's partly because you set the visual effect view's frame to the navigation bar's bounds. 部分原因是您将视觉效果视图的框架设置为导航栏的边界。 What you see in the screen shot are the navigation bar's bounds. 在屏幕快照看到的导航栏的边界。 So, you might be able to compensate by giving your visual effect view a different frame, ie move its origin up 20 points and increase its height. 因此,您可能可以通过为视觉效果视图提供不同的帧来进行补偿,例如,将其原点向上移动20个点并增加其高度。

It's a little unclear to me, though, why you don't just make the navigation bar translucent. 不过,对于我来说还不太清楚,为什么您不只是使导航栏变得半透明。 Navigation bar translucency is the blur effect, and it is supported. 导航栏的半透明性模糊效果,并且受支持。 What you're doing — adding a subview to the navigation bar — is not. 您正在执行的操作(向导航栏添加子视图)不是。

Managed to get it working adding this: 设法使其正常运行,添加以下内容:

bounds.offsetInPlace(dx: 0.0, dy: -20.0)
bounds.size.height = bounds.height + 20.0

As for this problem, I have modified Kampai's earlier answer, which the original was posted here . 对于这个问题,我修改了Kampai的早期答案,该答案已在此处发布。

The code below would blur the status bar as well as navigation bar. 下面的代码将模糊状态栏和导航栏。

Swift 4: 斯威夫特4:

func navigationBarBlur() {
    let bounds = CGRect(x: 0.0, y: -20.0, width: UserDefaults.screenWidth, height: (self.navigationController?.navigationBar.bounds.size.height)! + 20.0)
    let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
    self.visualEffectView = visualEffectView
    visualEffectView.frame = bounds
    visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    self.navigationController?.navigationBar.addSubview(visualEffectView)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM