的UISearchBar取消按钮没有响应(UISearchBar cancel button not

2019-08-16 23:48发布

I have implemented searchbar which shows cancel button once user has focus in searchbar. For this I have written searchBar.showsCancelButton = YES; in my searchBarTextDidBeginEditing method. In searchBarSearchButtonClicked, I resign the keyboard so that user can view full tableview.

Problem: At this point the searchbar cancel button is not responding. It only responds once searchbar gets focus again. Is this the default property of search bars cancel button or am I missing something in my code. I want to use cancel button without giving the focus in searchbar again.

Answer 1:

这是搜索栏取消按钮的默认行为。 如果你想其他的功能,你可以取消cancelbutton属性搜索栏和可以使用UIButton作为取消按钮。



Answer 2:

是的,你可以让你UISearchBar取消按钮响应。 默认情况下它是禁用,但您可以访问它的子视图,然后设置允许取消按钮。

for (id object in [mySearchBar subviews])
{
    if ([object isKindOfClass:[UIButton class]])
    {
        UIButton *searchBarCancelBtn = (UIButton*)object;
        [searchBarCancelBtn setEnabled:YES];
    }
}

如果你想一些其他的逻辑按照这个答案自定义清楚按钮的UISearchBar文本字段



Answer 3:

我认为它的默认行为UISearchBar时,它得到然后才把取消按钮将被启用焦点。 你不能得到取消的按钮事件UISearchBar时,它没有焦点。

所以我suggession实现你的任务是,

  • 创建一个工具栏
  • 插入UIBarButtonItem自定义视图UISearchBar
  • 插入UIBarButtonItem命名为“取消”用行动


文章来源: UISearchBar cancel button not responding