(IOS)ios11之后弹出弹框AlertView之后,点击页面空白处,弹框不消失问题

ios11之后弹出弹框AlertView之后,点击页面空白处,弹框不消失问题,原因是UIAlertController在iOS11之后底层视图继承关系有了新的变换,详情请看官方文档。

解决方案就是自定义AlertController,添加点击手势。


1.自定义UIAlertController

.h代码

#import

@interfaceBaseAlertController :UIAlertController

@end

.m代码

#import "BaseAlertController.h"

@interface BaseAlertController ()

@property (nonatomic,strong) UITapGestureRecognizer *closeGesture;

@end

@implementationBaseAlertController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.closeGesture= [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(closeAlert:)];

}

- (void)viewDidAppear:(BOOL)animated {

    [superviewDidAppear:animated];

    UIView*superView =self.view.superview;

    if(![superView.gestureRecognizerscontainsObject:self.closeGesture]) {

        [superView.subviews[0]addGestureRecognizer:self.closeGesture];

        superView.subviews[0].userInteractionEnabled = YES;

    }

}

- (void)closeAlert:(UITapGestureRecognizer*) gesture{

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end


2.使用方法 (和原生方法一样调用即可)

BaseAlertController*alertController = [BaseAlertControlleralertControllerWithTitle:@"确认手机号码"message:[NSStringstringWithFormat:@"我们将发送验证码短信到这个号码:+86%@",self.phoneTextField.text] preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

    }];

    [cancelActionsetValue:[UIColor colorWithHexString:@"#999999"] forKey:@"titleTextColor"];

    [alertControlleraddAction:cancelAction];


    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

        [self sendSmsCodeRequest];

    }];

    [sureActionsetValue:[UIColor colorWithHexString:@"#E7380C"] forKey:@"titleTextColor"];


    [alertControlleraddAction:sureAction];


    // 由于它是一个控制器 直接modal出来就好了

    [self presentViewController:alertController animated:YES completion:nil];

你可能感兴趣的:((IOS)ios11之后弹出弹框AlertView之后,点击页面空白处,弹框不消失问题)