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

蹲街弑〆低调 提交于 2019-12-23 05:22:35

问题


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)

回答1:


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, i.e. move its origin up 20 points and increase its height.

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.




回答2:


Managed to get it working adding this:

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



回答3:


As for this problem, I have modified Kampai's earlier answer, which the original was posted here.

The code below would blur the status bar as well as navigation bar.

Swift 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)
}


来源:https://stackoverflow.com/questions/33067313/real-time-blur-effect-for-navigation-bar-status-bar-not-getting-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!