(IOS)ios11之后彈出彈框AlertView之后,點(diǎn)擊頁(yè)面空白處,彈框不消失問題

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];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容