我正在使用带有UISearchBar的UITableViewController。 UITableViewController嵌入在UINavigationController中。 显示表格视图时,搜索栏按预期位于顶部(在导航栏下方)。 导航栏显示标题和提示。
当我单击搜索栏时,它会向上移动并隐藏导航栏中的标题。 提示仍然可见。 当我不使用提示时,搜索栏在我点击它之后仍然按照预期位于导航栏下方。
我创建了一个非常简单的项目来演示我的问题。 该项目具有UITableViewController的以下文件:
import UIKit class searchTVC: UITableViewController, UISearchControllerDelegate, UISearchResultsUpdating { fileprivate let searchController = UISearchController(searchResultsController: nil) override func viewDidLoad() { super.viewDidLoad() title = "Title" navigationItem.prompt = "prompt" tableView.backgroundColor = UIColor.gray // Add search bar searchController.delegate = self searchController.searchResultsUpdater = self searchController.hidesNavigationBarDuringPresentation = false searchController.dimsBackgroundDuringPresentation = false searchController.searchBar.sizeToFit() tableView.tableHeaderView = searchController.searchBar // remove separators for empty table tableView.tableFooterView = UIView() } func updateSearchResults(for searchController: UISearchController) { // ignore } }在故事板中,只有一个空的UITableViewController嵌入在UINavigationController中。 结果如下所示。
如果您注释掉设置提示的行,则行为符合预期。
我最近将我的代码更新为Swift 4.我不记得在使用Swift 3时我看到了这种行为,但我不完全确定它是用Swift 4引入的。
我在代码中遗漏了什么,或者这是一个错误?
I am using a UITableViewController with a UISearchBar. The UITableViewController is embedded in a UINavigationController. When the table view is shown the search bar is at the top as expected (below the navigation bar). The navigation bar is showing a title and a prompt.
When I click the search bar, it is moved up and hides the title in my navigation bar. The prompt is still visible. When I do not use a prompt, the search bar is still below the navigation bar as expected after I clicked it.
I have created a very simple project to demonstrate my problem. The project has the following file for a UITableViewController:
import UIKit class searchTVC: UITableViewController, UISearchControllerDelegate, UISearchResultsUpdating { fileprivate let searchController = UISearchController(searchResultsController: nil) override func viewDidLoad() { super.viewDidLoad() title = "Title" navigationItem.prompt = "prompt" tableView.backgroundColor = UIColor.gray // Add search bar searchController.delegate = self searchController.searchResultsUpdater = self searchController.hidesNavigationBarDuringPresentation = false searchController.dimsBackgroundDuringPresentation = false searchController.searchBar.sizeToFit() tableView.tableHeaderView = searchController.searchBar // remove separators for empty table tableView.tableFooterView = UIView() } func updateSearchResults(for searchController: UISearchController) { // ignore } }In the storyboard there is only an empty UITableViewController that is embedded in an UINavigationController. The result is shown below.
If you comment out the line that sets the prompt the behavior is as expected.
I recently updated my code to Swift 4. I cannot recall that I saw this behavior when using Swift 3, but I am not completely sure that it was introduced with Swift 4.
Am I missing something in the code or is this a bug?
最满意答案
在viewDidLoad设置这些:
self.definesPresentationContext = true self.edgesForExtendedLayout = .bottomSet these in viewDidLoad:
self.definesPresentationContext = true self.edgesForExtendedLayout = .bottom更多推荐
发布评论