Real time blur effect for Navigation Bar

后端 未结 9 2301
不思量自难忘°
不思量自难忘° 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:00

    Swift 4

    extension UINavigationBar {
        func installBlurEffect() {
            isTranslucent = true
            setBackgroundImage(UIImage(), for: .default)
            let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height
            var blurFrame = bounds
            blurFrame.size.height += statusBarHeight
            blurFrame.origin.y -= statusBarHeight
            let blurView  = UIVisualEffectView(effect: UIBlurEffect(style: .light))
            blurView.isUserInteractionEnabled = false
            blurView.frame = blurFrame
            blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            addSubview(blurView)
            blurView.layer.zPosition = -1
        }
    }
    

    Usage

    navigationController?.navigationBar.installBlurEffect()
    

提交回复
热议问题