UIAlertController 的使用方法這里就不介紹了 .
直接上思路,UIAlertController類比一個控制器.我們在顯示UIAlertController時用到
[self presentViewController:alertController animated:YES completion:nil];
我們可以在模態(tài)進去之后添加一個點擊手勢.執(zhí)行點擊時就把自己隱藏起來.Perfect
做一個UIAlertController 的分類. 先聲明
@interface UIAlertController (Extension)
-(void)tapGesAlert;
方法實現
-(void)tapGesAlert
{
NSArray * arrayViews = [UIApplication sharedApplication].keyWindow.subviews;
if (arrayViews.count>0) {
//array會有兩個對象,一個是UILayoutContainerView,另外一個是UITransitionView,我們找到最后一個
UIView * backView = arrayViews.lastObject;
//我們可以在這個backView上添加點擊事件,當然也可以在其子view上添加,如下:
// NSArray * subBackView = [backView subviews];
// backView = subBackView[0]; 或者如下
// backView = subBackView[1];
backView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(CancelAlert)];
[backView addGestureRecognizer:tap];
}
}
-(void)CancelAlert
{
[self dismissViewControllerAnimated:YES completion:nil];
}
接下來需要注意的是.
[self presentViewController:alertController animated:YES completion:nil];
// 以往使用的要換成下邊的.才能完成添加手勢
[self presentViewController:alertController animated:YES completion:^{
[alertController tapGesAlert];
}];
//還有.也可以把取消事件開放出來
@interface UIAlertController (Extension)
-(void)tapGesAlert;
-(void)CancelAlert;
這樣子就行了.趕緊去試試吧