Real time blur effect for Navigation Bar

后端 未结 9 2245
不思量自难忘°
不思量自难忘° 2020-11-28 05:21

How to achieve the real time blurring effect for the navigation bar just like the Trailers app in iPhone.

i.e As you scroll the contents should get blurred behind th

相关标签:
9条回答
  • 2020-11-28 06:21

    If, after @Kampai answer you get the status bar not getting in the effect, add this:

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

    Question addressed in this topic.

    0 讨论(0)
  • 2020-11-28 06:22

    I've added @Kampai's,@Damasio's with my tweaks to resolve my issues(which was pushNavigation related).Code will support Swift 4.0+, iOS9, Xcode 9

    In your ViewController's ViewDidLoad(), just call

    addBlurEffect(toView: self.navigationController?.navigationBar)
    

    function:

       //MARK :- It can be used in navBarGlassEffect view
    func addBlurEffect(toView view:UIView?) {
        // Add blur view
        guard let view = view else { return }
    
    
        //This will let visualEffectView to work perfectly
        if let navBar = view as? UINavigationBar{
            navBar.setBackgroundImage(UIImage(), for: .default)
            navBar.shadowImage = UIImage()
        }
    
    
        var bounds = view.bounds
        bounds.offsetBy(dx: 0.0, dy: -20.0)
        bounds.size.height = bounds.height + 20.0
    
    
        let blurEffect = UIBlurEffect(style: .dark)
        let visualEffectView = UIVisualEffectView(effect: blurEffect)
    
        visualEffectView.isUserInteractionEnabled = false
        visualEffectView.frame = bounds
        visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.insertSubview(visualEffectView, at: 0)
    
    }
    
    0 讨论(0)
  • 2020-11-28 06:24

    I first added addBlurEffect() method and then in AppDelegate, I added

     UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
    
     UINavigationBar.appearance().shadowImage = UIImage()
    
     UINavigationBar.appearance().backgroundColor = UIColor.clearColor()
    
     UINavigationBar.appearance().translucent = true
    

    Now it works for me

    0 讨论(0)
提交回复
热议问题