我有在 searchBarCancelButtonClicked:
中运行的代码 我想在用户点击搜索栏以外的位置时运行相同的代码,即使不一定点击取消。我当前的代码在下面,但有一个我无法弄清楚的问题。我在搜索栏下方放置了一个 UIView,单击它时会运行一个执行我想要的操作的方法。
问题是,当搜索栏处于事件状态时,与 uisearchbar 关联的半透明 View 会出现在我的 View 顶部,单击该 View 仍然只会关闭搜索栏,但不会运行我的方法。关闭搜索栏后,单击 View 会运行我的方法,所以我知道这没问题。基本上,当搜索栏是第一响应者时,我需要能够激活查看手势。
- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar {
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissSearch:)];
UIView *transparentView = [[UIView alloc]initWithFrame:transparentViewRect];
[transparentView setBackgroundColor:[UIColor blackColor]];
[transparentView addGestureRecognizer:singleTap];
[self.view addSubview:transparentView];
[self.view bringSubviewToFront:transparentView];
return YES;
}
- (void) dismissSearch:(UITapGestureRecognizer *)tapGesture
{
//Run my code
}
最佳答案
我想您可能可以使用 searchBarShouldEndEditing:
或 searchBarTextDidEndEditing:
委托(delegate)方法...
关于ios - 关闭 UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14736206/