Vineeth Krishnan Vineeth Krishnan - 4 months ago 71
Swift Question

How to blur the viewcontroller when alertcontroller is present?

Is it possible to do this ?? I have a UIAlertController which appears in a viewcontroller.What I wish to acheive is to blur/cover the background when the alert is present and once the alert is gone the background should be visible

Answer

To show UIAlertController background as blurred use UIVisualEffectView as:

 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)

Screenshots

Before showing alert view                                      When alert view is visible

enter image description here enter image description here

Comments