2016-07-27 50 views
1

是否有可能這樣做?我有出現在viewcontroller.What我想acheive一個UIAlertController是模糊/覆蓋背景當警報存在,一旦告警消失的背景應該是可見如何在alertcontroller存在時模糊viewcontroller?

進口的UIKit 進口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) 
} 

}

回答

4

內要顯示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) 

截圖

之前顯示警報視圖                                                             當警報視圖可見

enter image description hereenter image description here

+0

我已經加入了code.The回答您發佈工作正常的alertcontroller.Please你能幫我在哪裏調用self.view.addSubview(blurVisualEffectView) 的authenticateUser()方法內 –

+1

在'blurVisualEffectView.frame = view.bounds'行之後調用'self.view.addSubview(blurVisualEffectView)',並確保在else條件中移除'blurVisualEffectView'。 –

+0

謝謝噸sunil –

0

你可以通過創建一個具有透明背景和它UIVisualEffectView一個模式視圖控制器做到這一點。讓它呈現在當前視圖控制器的上下文中,然後讓它顯示警報。

這樣一來,視圖控制器就會在視圖控制器顯示警報之前使視圖模糊。

2

您可以將UIVisualEffectView添加到您的viewControllerview屬性時alert存在,並且當alert被駁回刪除此blurView

var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) 
var blurEffectView = UIVisualEffectView(effect: blurEffect) 
blurEffectView.frame = view.bounds 
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] // for supporting device rotation 
view.addSubview(blurEffectView) 

當你的alert被駁回,只需要調用blurEffectView.removeFromSuperView()

0

使用UIVisualEffectView設置一個視圖面具戴在你的內容,然後在模糊視圖的頂部添加警報。

這裏是一個例子

private var _blurView: UIVisualEffectView? 

那麼你的類

func layoutBlurView(){ 
    if(self._blurView == nil){ 
     let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light) 
     self._blurView = UIVisualEffectView(effect: blurEffect) 
     self.view.insertSubview(self._blurView!, belowSubview: alertView) 
    } 
    self._blurView!.frame = self.view.bounds 
} 

其中alertView是....你的警報視圖

1

在迅速3。1

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light) 
    let blurEffectView = UIVisualEffectView(effect: blurEffect) 
    blurEffectView.frame = view.bounds 
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
    self.view.addSubview(blurEffectView) 

    let previewController = UIAlertController(title: "Following", message: "Hello", preferredStyle: .alert) 

    let followAction = UIAlertAction(title: "Follow", style: .default, handler: { (UIAlertAction) in 
     blurEffectView.removeFromSuperview() 
    }) 

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: { (UIAlertAction) in 
     blurEffectView.removeFromSuperview() 
    }) 

    previewController.addAction(cancelAction) 
    previewController.addAction(followAction) 

    present(previewController, animated: true, completion: nil)