UISearchController:即使搜索栏为空,也会显示结果 [英] UISearchController: show results even when search bar is empty

查看:620
本文介绍了UISearchController:即使搜索栏为空,也会显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知, UISearchController 的默认行为是:

As I understand, the default behaviour of UISearchController is:


  1. On点击搜索栏,背景变暗,并显示取消按钮。直到此时才显示 SearchResultsController

  2. SearchResultsController 仅在以下情况下显示:搜索栏不为空。

  1. On tapping search bar, background is dimmed and 'cancel' button is shown. SearchResultsController is not shown till this point.
  2. SearchResultsController is displayed only if search bar is not empty.

即使在搜索时我想显示 SearchResultsController bar是空的但是被选中(即上面的情况1)。

I want to display SearchResultsController even when search bar is empty but selected (i.e is case 1 above).

简单地说,我想显示搜索结果而不是背景调光。

Simply put, instead of background dimming, I would like to show Search results.

有没有办法做到这一点?

Is there a way for doing this?

更多澄清:

我没有使用 UISearchController 来过滤显示它的视图上显示的结果,但是其他一些不相关的结果。
这就像facebook在其新闻Feed中所做的那样。点击搜索栏最初会显示搜索建议,然后,当我们开始编辑时,它会显示可能与新闻Feed无关的搜索结果。

I am not using UISearchController to filter results shown on the view on which it is shown, but some other unrelated results. It will be like what facebook does on its 'News Feed'. Tapping on search bar shows search suggestions initially and then, when we start editing, it shows search results which might not be related to news feed.

推荐答案

如果您的searchBar处于活动状态但没有文本,则会显示基础tableView结果。这是内置行为,以及为此状态隐藏searchResultsController的原因。

If your searchBar is active but has no text, the underlying tableView results are shown. That's the built-in behavior, and the reason why searchResultsController is hidden for that state.

要在搜索处于活动状态但不进行过滤时更改行为,您将会必须在通常仍然隐藏的情况下显示searchResultsController。

To change the behavior when search is active but not filtering, you're going to have to show the searchResultsController when it is normally still hidden.

通过< UISearchResultsUpdating> updateSearchResultsForSearchController:。如果你可以通过协议解决它,那么这是首选的方式。

There may be a good way to accomplish this via <UISearchResultsUpdating> and updateSearchResultsForSearchController:. If you can solve it via the protocol, that's the preferred way to go.

如果这没有帮助,你就会被黑客攻击内置行为。我不会推荐或依赖它,它会变得脆弱,但如果您选择该选项,这里是一个答案:

If that doesn't help, you're left with hacking the built-in behavior. I wouldn't recommend or rely on it, and it's going to be fragile, but here's an answer if you choose that option:


  1. 确保tableViewController符合< UISearchControllerDelegate> ,并添加

self .searchController.delegate = self;

实现 willPresentSearchController:

- (void)willPresentSearchController:(UISearchController *)searchController
{
    dispatch_async(dispatch_get_main_queue(), ^{
        searchController.searchResultsController.view.hidden = NO;
    });
}

这使得 searchResultsController UISearchController 之后可见,将其设置为隐藏。

This makes the searchResultsController visible after its UISearchController set it to hidden.

实施 didPresentSearchController:

- (void)didPresentSearchController:(UISearchController *)searchController
{
    searchController.searchResultsController.view.hidden = NO;
}


For解决内置行为的更好方法,请参阅 malhal的回答

这篇关于UISearchController:即使搜索栏为空,也会显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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