以编程方式激活UISearchBar会阻止用户与其进行交互 [英] Programmatically activating UISearchBar blocks user interactions with it

查看:85
本文介绍了以编程方式激活UISearchBar会阻止用户与其进行交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在导航控制器中有一个简单的表视图,工具栏可见。在工具栏上是一个搜索按钮,表视图的表头视图设置为我的 UISearchBar 。如果我向上滚动表格视图,然后点击搜索栏以激活它,一切都正常。但是,如果我点击搜索按钮,以编程方式激活搜索栏,那么与搜索栏的所有交互都被禁用(看起来被某些东西阻止),用户无法清除搜索,取消搜索甚至导航文本域中的文本。

I have a simple table view inside a navigation controller with the toolbar visible. On the toolbar is a search button, and the table view's table header view is set to my UISearchBar. If I scroll the table view up and then tap on the search bar to activate it, everything happens normally. But if I tap on the search button, which activates search bar programmatically, then all interactions with the search bar are disabled (looks like it's blocked by something), and the user can't clear the search, cancel the search or even navigate around the text in the textfield.

-(void)viewDidLoad
{
    [self setupSearchBar];
}

-(void)setupSearchBar
{
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44.0)];
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeDefault;
    self.searchBar.delegate = self;
    self.tableView.tableHeaderView = self.searchBar;

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar
                                                          contentsController:self];
    self.searchController.delegate = self;
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;

    [self.searchController.searchResultsTableView registerNib:[UINib nibWithNibName:@"ItemCell" bundle:nil]
                                   forCellReuseIdentifier:CellIdentifier];

    for (UIView* subview in self.searchBar.subviews) {
        if ([subview conformsToProtocol:@protocol(UITextInputTraits)]) {
            [(UITextField*)subview setKeyboardAppearance:UIKeyboardAppearanceAlert];
            [(UITextField*)subview setReturnKeyType:UIReturnKeyDone];
        }
    }

    self.tableView.contentOffset = CGPointMake(0, self.searchBar.frame.size.height);
}

-(IBAction)search:(id)sender
{
    [self.searchBar becomeFirstResponder]; // also tried [self.searchController setActive:YES animated:YES];
} 

我设置了一个 NSFetchedResultsController 如这个SO帖子。

更新

我通过制作解决了这个问题我的 UITableView 在将 UISearchBar 设置为第一响应者之前滚动到顶部。为什么这样做?我不知道,看到成为第一响应者部分不是问题,但如果搜索栏在它成为第一响应者时不在屏幕上,则取消和清除按钮将不起作用。这是我的代码。

I worked around this problem by making my UITableView scroll to the top before setting the UISearchBar to first responder. Why does this work? I don't know, seeing that the becoming first responder part is not an issue, but if the search bar is not on the screen when it becomes first responder, the cancel and clear buttons won't work. This is my code now.

-(void)search:(id)sender
{
    [self.tableView setContentOffset:CGPointMake(0, -self.tableView.contentInset.top) animated:NO];
    double delayInSeconds = 0.1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self.searchController setActive:YES animated:YES];
        [self.searchController.searchBar becomeFirstResponder];
    });
}

如果有人对这有效的原因有任何见解,我都是耳朵。

If someone has any insights as to why this works, I'm all ears.

推荐答案

尝试执行点击测试,看你的视图是否受阻。

Try performing a hit test, to see if your view is obstructed.

[self.view hitTest:[self.view convertPoint:searchBar.center fromView:searchBar]] ;

这篇关于以编程方式激活UISearchBar会阻止用户与其进行交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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