我使用的是UITableViewController和UISearchBar。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中引入的。
我是在代码中遗漏了什么,还是这是一个bug?
发布于 2017-10-13 10:32:42
在viewDidLoad
中设置这些
self.definesPresentationContext = true
self.edgesForExtendedLayout = .bottom
https://stackoverflow.com/questions/46732839
复制