【问题标题】:Make UISearchController search bar automatically active使 UISearchController 搜索栏自动激活
【发布时间】:2015-07-07 16:14:28
【问题描述】:

我已经实现了一个 UISearchController,它的搜索栏位于导航栏中,我希望在加载视图时激活搜索栏。当我说活动时,我的意思是键盘出现并且用户可以在不点击搜索栏的情况下输入他/她的搜索。

我使用以下代码初始化了 UISearchController:

- (void)viewDidLoad
{
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    [self.searchController setSearchResultsUpdater:self];
    [self.searchController setDimsBackgroundDuringPresentation:NO];
    [self.searchController setHidesNavigationBarDuringPresentation:NO];
    [self.navigationItem setTitleView:self.searchController.searchBar];

    [self setDefinesPresentationContext:YES];

    [self.searchController.searchBar setDelegate:self];
    [self.searchController.searchBar setShowsCancelButton:YES];
    [self.searchController.searchBar setPlaceholder:@"City or Airfield"];

    [self.searchController.searchBar sizeToFit];
}

我尝试激活我的搜索控制器,调用[self.searchController.searchBar becomeFirstResponder] 并直接调用searchBarSearchButtonClicked,但没有任何效果。

【问题讨论】:

  • is searchController 代码在 viewDidLoad 中
  • 是的(我已经编辑了帖子)
  • 只是为了确保在您点击搜索栏时使用当前代码是否一切正常......对吗?你把“[self.searchController.searchBar becomeFirstResponder]”放在 viewDidLoad 的什么地方?
  • 是的,一切正常。我首先在 viewDidLoad 中尝试,然后在 viewDidAppear 中尝试了相同的结果,没有。
  • 所以你已经在初始化搜索栏后放置了'[self.searchController.searchBar becomeFirstResponder]',对吗?

标签: ios objective-c uisearchbar uisearchcontroller


【解决方案1】:

尝试调用

[self.searchController setActive:YES]

之前

[self.searchController.searchBar becomeFirstResponder]

如果上述方法不起作用,请尝试以下操作:

- (void)viewDidLoad {
    [super viewDidLoad];
    ...
    [self initializeSearchController];
    ....
}

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

- (void)initializeSearchController {
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.delegate = self;
    self.searchController.searchBar.delegate = self;
    [self.searchController.searchBar sizeToFit];

    [self.tableView setTableHeaderView:self.searchController.searchBar];
    self.definesPresentationContext = YES;
}

【讨论】:

  • 我仍然有相同的结果。我不明白的是,尽管我将 active 设置为 YES,但当我打印 self.searchController.active 时仍然设置为 NO。
  • 实际上对我来说,您的代码看起来不错,但您可能错过了将 Active 设置为搜索控制器。我已经更新了我的答案,分成几部分。希望它可以帮助......试试吧。
  • 还是一样的结果。我意识到我忘了提到我正在使用模态转场,你认为这可能是个问题吗?
  • 很难说,因为我没有看到相关的代码。我现在正在考虑您之前提到的搜索控制器从未处于活动状态,尽管设置处于活动状态。因此,如果可能的话,您可以尝试另一种方法,添加带有搜索栏的子视图并将其放置在导航栏中。
  • 这不适用于 iOS 9。这是我的解决方案:stackoverflow.com/a/37848770/3299612
【解决方案2】:

要激活搜索栏:

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

可以调用"becomeFistResponder" 使键盘仅在 UISearchController 加载时出现。

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

【讨论】:

  • 请不要添加明码作为答案。解释您的答案如何解决 OP 问题。
  • 已更新说明。
  • 由于某种原因,在viewDidLoad 中设置[searchController setActive:YES]; 不起作用。这是因为 searchController.searchBar 没有以某种方式完全初始化。 searchController.searchBar 也可能存在与视图相关的问题。这个解决方案也对我有用。
  • 我需要在 didPresentSearchController 中添加一个延迟,以使其在 iOS 12 模拟器中运行。我添加了 300 毫秒的延迟,我认为这是合理的。
【解决方案3】:

除了按照其他用户的建议做之外,我还做了以下事情,并且成功了:

searchController.definesPresentationContext = YES;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 2013-10-21
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    相关资源
    最近更新 更多