如何使导航栏和状态栏模糊(UIBlurEffect)?iOS,斯威夫特 3 [英] How to make a Navigation Bar and Status Bar blurred (UIBlurEffect)? iOS, Swift 3

查看:44
本文介绍了如何使导航栏和状态栏模糊(UIBlurEffect)?iOS,斯威夫特 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让导航栏和状态栏变得模糊(UIBlurEffect)?当我通过点击图片向下滚动(Scroll View)到其他图片时,这张图片(在这种情况下是白色机器)在导航栏下只是丢失了,并且有必要在导航栏下看到这个图具有 UIBlurEffect 效果的导航栏.

How to make the Navigation Bar and Status Bar blurred (UIBlurEffect)? When I'm by clicking on the image to scroll down (Scroll View) to other pictures, this picture (in this case with a white machine) is simply lost under the Navigation Bar, and it is necessary that this figure would be visible under the Navigation Bar with effect UIBlurEffect.

无 UIBlurEffect

我试过了,但没有用:

func addBlurEffect() {
// Add blur view
let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = bounds!
visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)
visualEffectView.isUserInteractionEnabled = false }

首先,当滚动图片时,导航栏下的图片就会消失.
其次,状态栏保持灰色.

Firstly, when scrolling the picture just disappears under the Navigation Bar.
Second, Status Bar remains gray.

导航栏的 UIBlurEffect 不好,状态栏没有 UIBlurEffect

为了不让Status Bar一直是灰色的,我试了试,但无济于事=(

In order to Status Bar not remain gray, I tried to do it, but to no avail =(

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

也在didFinishLaunchingWithOptions中的AppDelegate(他用Objective-C编写的应用程序)中,我尝试添加它保持不变,没有任何变化:

Also in AppDelegate (he application written in Objective-C) in didFinishLaunchingWithOptions, I tried to add it all remains the same without any changes:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc]init]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f]];
[[UINavigationBar appearance] setTranslucent:YES];

请帮忙解决问题,我折腾了3天.
抱歉我的英语不好.

Please help solve the problem, I mess around with it for 3 days.
Sorry for my bad English.

推荐答案

离开 @Vignesh 回答 硬编码 -10 是个坏主意.例如,这对于 iphone X 的大小将不正确.

Going off @Vignesh answer it's a bad idea to hard code -10. For example this won't be sized correctly for iphone X.

// Find size for blur effect.
let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
let bounds = self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -(statusBarHeight)).offsetBy(dx: 0, dy: -(statusBarHeight))
// Create blur effect.
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = bounds
// Set navigation bar up.
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)

我还建议创建 UINavigationController 的子类,因为这是一种很好的做法.像这样:

I would also recommend creating a subclass of UINavigationController as it's good practice. Something like this:

final class func MyCustomNavigation: UINavigationController {

    override func viewDidLoad() {

         // Find size for blur effect.
         let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
         let bounds = navigationBar.bounds.insetBy(dx: 0, dy: -(statusBarHeight)).offsetBy(dx: 0, dy: -(statusBarHeight))
         // Create blur effect.
         let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
         visualEffectView.frame = bounds
         // Set navigation bar up.
         navigationBar.isTranslucent = true
         navigationBar.setBackgroundImage(UIImage(), for: .default)
         navigationBar.addSubview(visualEffectView)
         navigationBar.sendSubview(toBack: visualEffectView)

    }

}

这篇关于如何使导航栏和状态栏模糊(UIBlurEffect)?iOS,斯威夫特 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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