我将一个模态视图控制器呈现为一个表单,并在单击取消按钮(即一个条形按钮项目)时将其解除 . 当我在该视图之外点击时,我需要忽略它 . 请帮我一个参考 . 注意:我的模态视图控制器带有导航控制器 .
@cli_hlt,@ Bill Brasky感谢您的回答 . 当在作为表单的模态视图之外发生敲击时,我需要忽略它 . 我在下面粘贴我的代码 .
-(void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
{
if(adminMode)
{
CHEditEmployeeViewController *editVC = [[CHEditEmployeeViewController alloc] initWithNibName:@"CHEditEmployeeViewController" bundle:nil];
editVC.delegate = self;
editVC.pickedEmployee = employee;
editVC.edit = TRUE;
editVC.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:editVC];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:navigationController animated:YES];
return;
} //the above code is from the view controller which presents the modal view. Please look at the below code too which is from my modal view controller. Please guide me in a proper way. -(void)tapGestureRecognizer {
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
[recognizer setNumberOfTapsRequired:1];
recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
[self.view addGestureRecognizer:recognizer];
}
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window
//Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view.
if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil])
{
[self dismissModalViewControllerAnimated:YES];
[self.view.window removeGestureRecognizer:sender];
}
}
}
13 回答
喔好吧 . 所以我担心使用presentModalViewController:方法不太可能 . 一个"modal"视图/窗口/消息框/等的整个想法 . pp . is 用户 cannot 除了处理任何视图/窗口/消息框/等之外还做任何其他事情 . pp . 希望他/她做 .
你要做的不是模态视图控制器,而是以常规方式加载和显示表单视图控制器 . 请注意主控制器中的表单只显示例如使用BOOL变量,然后处理可能发生的任何点击 . 如果您的表单正在显示,请将其解雇 .
我知道这是一个古老的问题,但这是可能的,尽管“正确”的答案说的是 . 由于这是我在寻找这个时的第一个结果,所以我决定详细说明:
这是你如何做到的:
您需要向View Controller中添加一个属性,您希望以模态方式呈现该属性,在我的情况下为“tapBehindGesture” .
然后在
viewDidAppear
这是tapBehindDetected的实现
请记住在
viewWillDisappear
上从view.window
删除tapBehindGesture
以避免在未分配的对象中触发handleTapBehind .我通过向手势识别器添加委托解决了iOS 8问题
有这些回应
这适用于iOS 8 GM
据我所知,答案似乎都没有在任何情况下立即起作用 .
我的解决方案(从中继承或粘贴):
这是我的版本适用于iOS 7和iOS 8,不需要条件交换坐标:
对于iOS 8,您必须在每个Martino的答案中实现
UIGestureRecognizer
,并在横向时交换轻敲位置的(x,y)坐标 . 不确定这是否是由于iOS 8错误造成的 .Swift 4版本兼容纵向和横向 - 无需交换x,y .
}
基于Bart van Kuik's answer和NavAutoDismiss以及其他伟大的片段 .
用法:
@yershuachu's answer,在Swift 2中:
斯威夫特3
}
我在这种形式下使用它,在iOS 7.1和iOS 8.3上都没有任何问题 .
我制作了一个自动关闭iPad的navigationController
https://github.com/curciosobrinho/NavAutoDismiss
它与上面的代码相同,适用于iOS 8 .
所以,所有的积分都归于上面的人 .
我只是做得更通用,更容易使用 .
您只需将BOTH文件复制到项目中即可
导入头文件(.h)
并使用您的viewcontroller(您想要显示)作为rootViewController .
如何使用它: