ios11之后彈出彈框AlertView之后,點(diǎn)擊頁(yè)面空白處,彈框不消失問題,原因是UIAlertController在iOS11之后底層視圖繼承關(guān)系有了新的變換,詳情請(qǐng)看官方文檔。
解決方案就是自定義AlertController,添加點(diǎn)擊手勢(shì)。
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.使用方法 (和原生方法一樣調(diào)用即可)
BaseAlertController*alertController = [BaseAlertControlleralertControllerWithTitle:@"確認(rèn)手機(jī)號(hào)碼"message:[NSStringstringWithFormat:@"我們將發(fā)送驗(yàn)證碼短信到這個(gè)號(hào)碼:+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];
? ? // 由于它是一個(gè)控制器 直接modal出來(lái)就好了
? ? [self presentViewController:alertController animated:YES completion:nil];