如何在我的UINavigationBar后面获得模糊效果

时间:2016-06-22 01:22:47

标签: ios xcode swift uinavigationbar

我一直试图让我的scrollView出现在navigationBar下稍微模糊,就像苹果消息应用中的情况一样。我尝试了以下方法,但没有一种方法有效:

1:

self.navigationController?.navigationBar.isTranslucent = true

2:

self.navigationController?.navigationBar.barStyle = UIBarStyle.blackTranslucent

3:

self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)

4:

self.navigationController?.navigationBar.backgroundColor = UIColor.clear()

我已经阅读了许多上述方法变化的帖子,并尝试了大部分但无济于事。我认为设置isTranslucent = true的目的是获得完全相同的效果。还有什么我应该尝试的吗?基本上我只是想让我的导航栏略微透视。

3 个答案:

答案 0 :(得分:3)

UIVisualEffectView正是您所寻找的,并且经过充分展示here

let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
visualEffectView.frame = bounds
self.navigationController?.navigationBar.addSubview(visualEffectView)

那就是说,使用你的方法1和2也应该有效:

self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.topItem.title = @"Contacts";
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

enter image description here

答案 1 :(得分:0)

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = (self.navigationController?.navigationBar.bounds)!
    self.navigationController?.navigationBar.addSubview(blurEffectView)

答案 2 :(得分:0)

在这里,您可以为导航栏模糊效果添加此代码,此功能非常适合您

func navigationBlurEffect() {
    // 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]

    //here you can choose one 
    self.navigationController?.navigationBar.addSubview(visualEffectView)
    // Or
    /*
 If you find that after adding blur effect on navigationBar, navigation buttons are not visible then add below line after adding blurView code.
    */ 
    self.navigationController?.navigationBar.sendSubviewToBack(visualEffectView)

    // Here you can add visual effects to any UIView control.
    // Replace custom view with navigation bar in above code to add effects to custom view.
}

enter image description here

// **这仅适用于Image Blur代码 此代码可用于图像模糊效果

  func getImageBlur(image:UIImageView){
        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.frame = (image.bounds)
        blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        image.addSubview(blurEffectView)
    }