关闭 UISearchBar [英] Dismissing UISearchBar

查看:23
本文介绍了关闭 UISearchBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在 searchBarCancelButtonClicked 中运行的代码: 我想在用户点击离开搜索栏时运行相同的代码,即使不一定点击取消.我当前的代码在下面,但有一个我无法弄清楚的问题.我在搜索栏下方放置了一个 UIView,当点击它时会运行一个可以执行我想要的方法.

问题是,当搜索栏处于活动状态时,与 uisearchbar 关联的半透明视图出现在我的视图之上,单击该视图仍然会关闭搜索栏,但不会运行我的方法.关闭搜索栏后,单击视图会运行我的方法,所以我知道这没问题.基本上,当搜索栏是第一响应者时,我需要能够激活视图手势.

- (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];返回是;}-(无效)dismissSearch:(UITapGestureRecognizer *)tapGesture{//运行我的代码}

解决方案

我猜你大概可以使用 searchBarShouldEndEditing:searchBarTextDidEndEditing: 委托方法...>

I have code that runs in searchBarCancelButtonClicked: I want to run the same code when the user clicks away from the searchbar, even though cancel isn't necessarily clicked. My current code is below but has an issue that I can't figure out. I have placed a UIView underneath the search bar that when clicked runs a method that does what I want.

The issue is that when the searchbar is active, the semi-transparent view associated with uisearchbar comes up on top of my view and clicking the view still just dismisses the searchbar, but doesn't run my method. After the searchbar is dismissed, clicking the view does run my method so I know that is ok. Basically I need to be able to make the view gesture active while the searchbar is the first responder.

- (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
}

解决方案

I guess you can probably use the searchBarShouldEndEditing: or searchBarTextDidEndEditing: delegate methods...

这篇关于关闭 UISearchBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆