如何在alertcontroller存在时模糊viewcontroller?

Vin*_*nan 2 swift uialertcontroller

是否有可能做到这一点 ??我有一个出现在viewcontroller中的UIAlertController.我希望实现的是当警报出现时模糊/覆盖背景,一旦警报消失,背景应该是可见的

import UIKit import LocalAuthentication类TabBarViewController:UITabBarController {

@IBOutlet weak var noteTabBar: UITabBar!

override func viewDidLoad() {
super.viewDidLoad()
    self.authenticateUser()
    self.tabBar.hidden = false
    self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

    let userDefaults = NSUserDefaults.standardUserDefaults()
    userDefaults.setObject(false, forKey: "sendModeToggle")
    userDefaults.setObject("Avenir-Medium", forKey: "font")
     userDefaults.setObject(13, forKey:"fontSize")
            // Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

// MARK: Touch ID Authentication

func authenticateUser()
{
    let context = LAContext()
    var error: NSError?
    let reasonString = "Authentication is needed to access your app! :)"

    let blurEffect = UIBlurEffect(style: .Light)
    let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
    blurVisualEffectView.frame = view.bounds

    if context.canEvaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, error: &error)
    {

        context.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: reasonString, reply: { (success, policyError) -> Void in

            if success
            {
                print("Authentication successful! :) ")
                blurVisualEffectView.removeFromSuperview()

            }
            else
            {
                switch policyError!.code
                {
                case LAError.SystemCancel.rawValue:
                    print("Authentication was cancelled by the system.")
                /*case LAError.UserCancel.rawValue:
                    print("Authentication was cancelled by the user.")
                 */   
                case LAError.UserFallback.rawValue:
                    print("User selected to enter password.")
                    NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                        self.showPasswordAlert()
                    })
                default:
                    print("Authentication failed! :(")
                    NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
                        self.showPasswordAlert()
                    })
                }
            }

        })
    }
    else
    {
        print(error?.localizedDescription)
        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
            self.showPasswordAlert()
        })
    }


}

// MARK: Password Alert

func showPasswordAlert()
{

    let blurEffect = UIBlurEffect(style: .Light)
    let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
    blurVisualEffectView.frame = view.bounds


    let alertController = UIAlertController(title: "Touch ID Password", message: "Please enter your password.", preferredStyle: .Alert)

    let defaultAction = UIAlertAction(title: "OK", style: .Cancel) { (action) -> Void in

        if let textField = alertController.textFields?.first as UITextField?
        {
            if textField.text == "notes"
            {
                print("Authentication successful! :) ")
                blurVisualEffectView.removeFromSuperview()
            }
            else
            {
                self.showPasswordAlert()

            }
        }
    }
    alertController.addAction(defaultAction)

    alertController.addTextFieldWithConfigurationHandler { (textField) -> Void in

        textField.placeholder = "Password"
        textField.secureTextEntry = true
    }
    self.view.addSubview(blurVisualEffectView)
    self.presentViewController(alertController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

}

Sun*_*rma 8

要将UIAlertController背景显示为模糊,请使用UIVisualEffectView:

 let blurEffect = UIBlurEffect(style: .Light)
 let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
 blurVisualEffectView.frame = view.bounds

 let alertController = UIAlertController.init(title: "Title", message: "Message", preferredStyle: .Alert)

 alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action: UIAlertAction!) in
     print("Handle Ok logic here")
     blurVisualEffectView.removeFromSuperview()
 }))

 alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action: UIAlertAction!) in
     print("Handle Cancel Logic here")
     blurVisualEffectView.removeFromSuperview()
 }))
 self.view.addSubview(blurVisualEffectView)
 self.presentViewController(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

截图

显示警报视图之前显示警报视图时

在此输入图像描述 在此输入图像描述