如何让UISearchController启动隐藏?

时间:2022-03-01 00:15:57

I have added a UISearchController to my code using the following method:

我使用以下方法将UISearchController添加到我的代码中:

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.scopeButtonTitles = @[];
self.searchController.searchBar.delegate = self;

self.tableView.tableHeaderView = self.searchController.searchBar;
[self.searchController.searchBar sizeToFit];
self.definesPresentationContext = YES;

This creates my search controller and adds it to the top of my tableView. Annoyingly it starts visible though:

这将创建我的搜索控制器并将其添加到我的tableView顶部。令人讨厌的是它开始可见:

如何让UISearchController启动隐藏?

I can hide it by sliding it up under the navigation bar which suggests the underlying functionality of the code is working but I can't get it to start hidden so I can slide it down.

我可以通过在导航栏下滑动来隐藏它,这表明代码的基本功能正在运行,但我无法让它开始隐藏,所以我可以将其滑下来。

I have tried adjusting the edge insets, I have tried setting the navigation bar to translucent, I have tried to go through the search bar tutorials online but nothing seems to be dealing with this issue.

我已经尝试调整边缘插图,我已经尝试将导航栏设置为半透明,我已经尝试通过在线搜索栏教程但似乎没有处理这个问题。

Any help very welcome

任何帮助都非常欢迎

3 个解决方案

#1


4  

Did you try setting the content offset of your table view?

您是否尝试设置表格视图的内容偏移量?

[self.tableView setContentOffset:CGPointMake(0, self.searchController.searchBar.frame.size.height) animated:NO];

#2


1  

Here is for swift 4

这是swift 4

tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.size.height), animated: true)

#3


1  

From iOS 11.0 onwards you can use,

从iOS 11.0起,您可以使用,

self.navigationItem.searchController = self.searchController;

The search bar will be hidden, unless you swipe down to reveal it.

搜索栏将被隐藏,除非您向下滑动以显示它。

#1


4  

Did you try setting the content offset of your table view?

您是否尝试设置表格视图的内容偏移量?

[self.tableView setContentOffset:CGPointMake(0, self.searchController.searchBar.frame.size.height) animated:NO];

#2


1  

Here is for swift 4

这是swift 4

tableView.setContentOffset(CGPoint(x: 0, y: searchController.searchBar.frame.size.height), animated: true)

#3


1  

From iOS 11.0 onwards you can use,

从iOS 11.0起,您可以使用,

self.navigationItem.searchController = self.searchController;

The search bar will be hidden, unless you swipe down to reveal it.

搜索栏将被隐藏,除非您向下滑动以显示它。