ios 在 UIAlertController 之外点击时如何关闭 UIAlertController?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30075832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dismiss UIAlertController when tap outside the UIAlertController?
提问by leizh00701
How to dismiss UIAlertController
when tap outside the UIAlertController
?
UIAlertController
在外面点击时如何关闭UIAlertController
?
I can add a UIAlertAction
of style UIAlertActionStyleCancel
to dismiss the UIAlertController
.
我可以添加一个UIAlertAction
样式UIAlertActionStyleCancel
来关闭UIAlertController
.
But I want to add the function that when user tap outside the UIAlertController
the UIAlertController
will dismiss. How to do that? Thank you.
但我想补充一点,当外面的使用者开关功能UIAlertController
的UIAlertController
将开除。怎么做?谢谢你。
采纳答案by Vivek Yadav
Add a separate cancel action with style UIAlertActionStyleCancel
. So that when user taps outside, you would get the callback.
添加一个单独的带有 style 的取消操作UIAlertActionStyleCancel
。这样当用户点击外面时,你会得到回调。
Obj-c
对象-c
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"A Message" preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Called when user taps outside
}]];
Swift 5.0
斯威夫特 5.0
let alertController = UIAlertController(title: "Alert Title", message: "A Message", preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {
action in
// Called when user taps outside
}))
回答by chancyWu
If you are targeting devices having iOS > 9.3 and using Swift and preferredStyle is Alertyou can use snippet as below:
如果您的目标设备是 iOS > 9.3 并且使用 Swift 并且 preferredStyle 是Alert,您可以使用如下代码片段:
func showAlertBtnClicked(sender: UIButton) {
let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .Alert)
self.presentViewController(alert, animated: true, completion:{
alert.view.superview?.userInteractionEnabled = true
alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
})
}
func alertControllerBackgroundTapped()
{
self.dismissViewControllerAnimated(true, completion: nil)
}
With swift 3:
快速 3:
func showAlertBtnClicked(sender: UIButton) {
let alert = UIAlertController(title: "This is title", message: "This is message", preferredStyle: .alert)
self.present(alert, animated: true) {
alert.view.superview?.isUserInteractionEnabled = true
alert.view.superview?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.alertControllerBackgroundTapped)))
}
}
func alertControllerBackgroundTapped()
{
self.dismiss(animated: true, completion: nil)
}
回答by Kazi Abdullah Al Mamun
Swift, Xcode 9
斯威夫特,Xcode 9
Dismiss AlertController with cancel button
使用取消按钮关闭 AlertController
provide action to your alertController where UIAlertAction
's style is .cancel
向您的 alertController 提供操作,其中UIAlertAction
的样式为.cancel
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
Using this method alertController will be dismissed when user will tap to cancel action button as well as outside of the alertController.
当用户点击取消操作按钮以及在 alertController 之外时,使用此方法将关闭 alertController。
if you don't want user to dismiss alertController after touch up outside of alertController, disable user interaction of first subviews of alertController in completion closure of present method.
如果您不希望用户在 alertController 之外修改后关闭 alertController,请在当前方法的完成关闭中禁用 alertController 的第一个子视图的用户交互。
self.present(alertController, animated: true) {
alertController.view.superview?.subviews[0].isUserInteractionEnabled = false
}
Dismiss AlertController on touchup outside of Controller view
在控制器视图之外的修饰中关闭 AlertController
If you don't want cancel button in your controller view and want to dismiss controller when user touchup outside of controller view, do so
如果您不想在控制器视图中使用取消按钮,并希望在控制器视图之外的用户触摸时关闭控制器,请执行此操作
self.present(alertController, animated: true) {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissAlertController))
alertController.view.superview?.subviews[0].addGestureRecognizer(tapGesture)
}
@objc func dismissAlertController(){
self.dismiss(animated: true, completion: nil)
}
回答by Kevin Machado
If you are using Swift:
如果您使用的是Swift:
Add an action with addAction(_:)
and style:UIAlertActionStyle.Cancel
.
使用addAction(_:)
和添加操作style:UIAlertActionStyle.Cancel
。
The `handler will be called when ou tap on the button or outside the frame.
当您点击按钮或在框架外时,将调用`处理程序。
var alertVC = UIAlertController(...) // initialize your Alert View Controller
alertVC.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: {
(alertAction: UIAlertAction!) in
alertVC.dismissViewControllerAnimated(true, completion: nil)
}))
Objective-C:
目标-C:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:...];
[alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[alertVC dismissViewControllerAnimated:YES completion:nil];
}]];
回答by Muthukumar Chinnu
Swift 4:
斯威夫特 4:
Dismiss Action Sheet when User Taps outside Action Sheet created using UIAlertController
当用户在使用 UIAlertController 创建的操作表之外点击时关闭操作表
Code Snippet:
代码片段:
// Declare Action Sheet reference
var actionSheet: UIAlertController!
// Init and Show Action Sheet
func showActionSheetClicked(sender: UIButton) {
// Init Action Sheet
actionSheet = UIAlertController(title: "Title", message: "Message", preferredStyle: .actionSheet)
self.present(actionSheet, animated: true) {
// Enabling Interaction for Transperent Full Screen Overlay
self.actionSheet.view.superview?.subviews.first?.isUserInteractionEnabled = true
// Adding Tap Gesture to Overlay
self.actionSheet.view.superview?.subviews.first?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.actionSheetBackgroundTapped)))
}
}
// To dismiss Action Sheet on Tap
@objc func actionSheetBackgroundTapped() {
self.actionSheet.dismiss(animated: true, completion: nil)
}
回答by oskarko
The easiest way in Obj-C:
Obj-C 中最简单的方法:
UIAlertController *alert = [UIAlertController alertControllerWithTitle: ...
[self presentViewController:alert animated:YES completion:^{
[alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertControllerBackgroundTapped)]];
}];
and then:
进而:
- (void)alertControllerBackgroundTapped
{
[self dismissViewControllerAnimated: YES
completion: nil];
}
回答by songxing10000
- (void)addBackgroundDismissTapForAlert:(UIAlertController *)alert {
if (!alert.view.superview) {
return;
}
alert.view.superview.userInteractionEnabled = YES;
[alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(alertControllerBackgroundTapped)]];
for (UIView *subV in alert.view.superview.subviews) {
if (subV.width && subV.height) {
subV.userInteractionEnabled = YES;
[subV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(alertControllerBackgroundTapped)]];
}
}
}
- (void)alertControllerBackgroundTapped {
[self dismissViewControllerAnimated: YES
completion: nil];
}
回答by Michael Lorenzo
If you view debug the superview of the alert, you see its not as simple as adding a tap gesture recognizer to the UITransitionView of the _UIAlertControllerView.You can do this instead
如果您查看调试警报的超级视图,您会发现它不像在 _UIAlertControllerView 的 UITransitionView 中添加点击手势识别器那么简单。您可以改为执行此操作
[presenter presentViewController:alertController animated:YES completion:^{
NSArray <UIView *>* superviewSubviews = alertController.view.superview.subviews;
for (UIView *subview in superviewSubviews) {
if (CGRectEqualToRect(subview.bounds, weakSelf.view.bounds)) {
[subview addSingleTapGestureWithTarget:weakSelf action:@selector(dismissModalTestViewController)];
}
}
}];
回答by Slashik
UIView *alertView = self.alertController.view;
UIView *superPuperView = self.alertController.view.superview;
CGPoint tapCoord = [tap locationInView:superPuperView];
if (!CGRectContainsPoint(alertView.frame, tapCoord)) {
//dismiss alert view
}
回答by Ibrahim
The simplest way:
最简单的方法:
- (void)viewDidLoad {
[super viewDidLoad];
[self button];
}
- (void) button {
UIButton * AlertButton = [UIButton buttonWithType:UIButtonTypeSystem];
[AlertButton setTitle:@"Button" forState:UIControlStateNormal];
AlertButton.frame = CGRectMake((self.view.frame.size.width/2) - 50 , (self.view.frame.size.height/2) - 25, 100, 50);
[AlertButton addTarget:self action:@selector(Alert) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:AlertButton];
}
- (void)Alert {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController: alert animated: YES completion:^{ alert.view.superview.userInteractionEnabled = YES; [alert.view.superview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(DismissAlertByTab)]]; }];
}
- (void)DismissAlertByTab
{
[self dismissViewControllerAnimated: YES completion: nil];
}