在呈现Uialertview时模糊视图控制器

时间:2015-07-14 16:58:36

标签: ios objective-c

我想制作按下它的按钮会弹出UIAlertView。我想知道是否有可能模糊UIAlertView周围的所有内容,比如将模糊效果放在该视图控制器上。我花了2个小时寻找解决方案,但没有任何效果如我所料。我想在图片上做同样的事情

UIAlertView with blur background

2 个答案:

答案 0 :(得分:10)

您需要使用UIBlurEffectUIVisualEffectView以下是您的代码的样子:

- (IBAction)buttonClicked:(id)sender
{

    // Create blur effect
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

    // Add effect to an effect view
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    visualEffectView.frame = self.view.frame;


    self.alertController = [UIAlertController alertControllerWithTitle: @"Alert"
                                                           message: nil
                                                    preferredStyle: UIAlertControllerStyleAlert];


    [self.alertController addAction:[UIAlertAction actionWithTitle: @"Cancel" style: UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        [visualEffectView removeFromSuperview];

    }]];

     [self.view addSubview:visualEffectView];

    [self presentViewController: self.alertController animated: true  completion: nil];

}

UIVisualEffectView Class Reference

结果:

UIVisualEffectView

答案 1 :(得分:1)

Swift 3

@IBAction func buttonClicked(_ sender: Any) {
        // Create blur effect
    let blurEffect = UIBlurEffect(style: .light)
        // Add effect to an effect view
    let visualEffectView = UIVisualEffectView(effect: blurEffect)
    visualEffectView.frame = view.frame

    alertController = UIAlertController(title: "Alert", message: nil, preferredStyle: .alert)
    alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {(_ action: UIAlertAction) -> Void in
        visualEffectView.removeFromSuperview()
    }))

    view.addSubview(visualEffectView)
    present(alertController, animated: true, completion: { _ in })
}