当前位置:  开发笔记 > IOS > 正文

如何使导航栏和状态栏模糊(UIBlurEffect)?iOS,Swift 3

如何解决《如何使导航栏和状态栏模糊(UIBlurEffect)?iOS,Swift3》经验,为你挑选了2个好方法。

如何使导航栏和状态栏模糊(UIBlurEffect)?当我点击图像向下滚动(滚动视图)到其他图片时,这张图片(在这种情况下是白色机器)在导航栏下丢失了,这个图形必须在具有效果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 }

首先,当滚动图片时,导航栏下方会消失.
其次,状态栏仍然是灰色的.

NavigationBar的UIBlurEffect错误,StatusBar没有UIBlurEffect

为了状态栏不保持灰色,我试着这样做,但无济于事=(

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

同样在didFinishLaunchingWithOptions中的AppDelegate(他用Objective-C编写的应用程序)中,我尝试添加它,所有内容保持不变,没有任何更改:

[[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天.
对不起,我的英语不好.



1> Vignesh..:

将背景图像设置为导航栏就可以了.在swift 3中,以下代码适用于我.

    let visualEffectView   = UIVisualEffectView(effect: UIBlurEffect(style: .light))
    visualEffectView.frame =  (self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -10).offsetBy(dx: 0, dy: -10))!
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.addSubview(visualEffectView)

输出:

在此输入图像描述


@RayKing我遇到了同样的问题.我在这里找到了解决方案:http://stackoverflow.com/a/27316465只需添加:self.navigationController?.navigationBar.sendSubviewToBack(visualEffectView)放好效果后.

2> Alex Dunlop..:

@Vignesh回答这对硬代码-10是个坏主意。例如,对于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的子类,因为这是一种很好的做法。像这样:

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)

    }

}

推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有