问题描述
我的 UITableViewController
符合新的 UISearchControllerDelegate
以及 UISearchResultsUpdating
。
以下是搜索栏的设置代码:
Here is my setup code for the search bar:
override func viewDidLoad() {
var searchController = UISearchController(searchResultsController: self)
searchController.searchResultsUpdater = self
self.tableView.tableHeaderView = searchController.searchBar
self.definesPresentationContext = true
}
但是,在模拟器中运行时没有搜索栏在表头中,即使它在代码中指定。我还在 viewWillAppear
中尝试了此代码,但是再次没有显示搜索栏。
However, when running this in the simulator there is no search bar in the table header, even though it is specified in the code. I also tried this code in viewWillAppear
, but again no search bar was shown.
推荐答案
Apple工程师告知我,您必须为搜索栏添加一个框架。如果您打印搜索栏的框架,您会发现它的高度为零。所以这可能是Apple代码中的一个错误。
I was informed by an Apple Engineer that you must give the Search Bar a frame. If you print the frame of the search bar, you will notice it's height is zero. So this is probably a bug in Apple's code.
searchController.searchBar = CGRectMake(0.0, 0.0, 320.0, 44.0)
编辑:
文件指明你必须传递要显示结果的View Controller。要在你所在的同一个View Controller中显示它,传入nil。
The documentation specifies that you must pass in the View Controller that you want to display the results. To display this in the same View Controller you are in, pass in nil.
var searchController = UISearchController(searchResultsController: nil)
这篇关于UISearchControllerDelegate - 搜索栏在表头中不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!