How to use UIBlurEffect with modal View Controllers?

后端 未结 6 2321
夕颜
夕颜 2021-02-13 14:17

I have a UIViewController that presents another UIViewController modally. I want the modal view controller to have the blur/transparency that iOS 7 int

6条回答
  •  自闭症患者
    2021-02-13 14:57

    This is a combination of the above answers.

    This works for Interface builder in Xcode 7.2.1 (Swift 2.1.) for iOS 9.2.1.

    The code has been tested on the simulator and device.

    In Interface Builder:

    1. Click the storyboard seque
    2. As "Kind" set "Present Modally"
    3. As "Presentation" set "Over Current Context" NOTE: Setting the 3.rd step in you viewDidLoadof the presenting UIViewController will not work
    4. Dont forget to click your UIViewController and under Custom Class"- Class name it YOUTransparentModalVC (or whatever you want).

    In code:

    import UIKit
    
    let IMAGE_OVERLAY_NAME = "backgroundColorExample"
    
    class YOUTransparentModalVC: UIViewController
    {
        private var backgroundImageOverlayIV:UIImageView!
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
            self.setupTransparentView()
            self.setupDissmissingVCOnTap()
            self.setupBackgroudImage()
        }
    
        private func setupTransparentView()
        {
            self.view.backgroundColor       = UIColor.clearColor()
            let effect                      = UIBlurEffect(style: UIBlurEffectStyle.Light)
            let blurView                    = UIVisualEffectView(effect: effect)
            blurView.frame                  = self.view.bounds
            self.view.addSubview(blurView)
        }
    
        private func setupBackgroudImage()
        {
            self.backgroundImageOverlayIV            = UIImageView(frame: self.view.frame)
            self.backgroundImageOverlayIV.image      = UIImage(named: IMAGE_OVERLAY_NAME)
            self.view.addSubview(self.backgroundImageOverlayIV)
        }
    
        private func setupDissmissingVCOnTap()
        {
            let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissVC")
            view.addGestureRecognizer(tap)
        }
    
        func dismissVC()
        {
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    
    
    }
    

提交回复
热议问题