无法将searchBar设置为firstResponder

无法将searchBar设置为firstResponder

本文介绍了无法将searchBar设置为firstResponder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个searchBar我在tableviewcontroller中设置。我引用了类似的问题但我仍然无法将其设置为第一响应者。
在.h文件中:

I have a searchBar I'm setting in a tableviewcontroller. i've referenced this similar question UISearchBar cannot become first responder after UITableView did re-appear but am still unable to set it as first responder.In .h file:

@property (strong, nonatomic) UISearchController *searchController;
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;

查看didload:

self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;

self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
self.tableView.tableHeaderView = self.searchController.searchBar;

并且在viewDidAppear中:

And in viewDidAppear:

-(void)viewDidAppear:(BOOL)animated {
  [self.searchController setActive:YES];
  [self.searchController.searchBar becomeFirstResponder];
  [super viewDidAppear:animated];
}

当我转向视图时,searchBar会动画,但不会出现键盘。

When I segue to the view the searchBar animates, but no keyboard appears.

推荐答案

我也注意到了这个问题。似乎发生的是当searchController仍在加载时,对 becomeFirstResponder 的调用已完成。如果您注释掉 becomeFirstResponder ,您会注意到没有区别。所以我们需要一种在searchController'完成'加载后调用becomeFirstResponder的方法。

I noticed this issue too. What seems to happen is the call to becomeFirstResponder is done when the searchController is still 'loading'. If you comment out becomeFirstResponder you notice that there is no difference. So we need a way to call becomeFirstResponder after the searchController is 'done' loading.

当我看到各种委托方法时,我注意到有一个委托方法:

When I looked at various delegate methods I noticed there is a delegate method:

- (void)didPresentSearchController:(UISearchController *)searchController

在显示searchController之后立即调用此方法。然后我打电话给 becomeFirstResponder

This method is called right after the searchController has been presented. Then I make the call to becomeFirstResponder:

- (void)didPresentSearchController:(UISearchController *)searchController
{
    [searchController.searchBar becomeFirstResponder];
}

这解决了这个问题。您会注意到,当加载searchController时,搜索栏现在具有焦点。

This fixes the problem. You will notice that when the searchController is loaded, the searchbar now has focus.

这篇关于无法将searchBar设置为firstResponder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 19:26