IOS:UISearchBar部分覆盖带有提示的UINavigationBar
我将UITableViewController与UISearchBar一起使用。UITableViewController嵌入在UINavigationController中。显示表格视图时,搜索栏按预期位于顶部(导航栏下方)。导航栏将显示标题和提示 单击搜索栏时,它会上移并在导航栏中隐藏标题。提示仍然可见。 当我不使用提示时,在单击搜索栏后,搜索栏仍在导航栏的下方 我创建了一个非常简单的项目来演示我的问题。项目具有UITableViewController的以下文件:IOS:UISearchBar部分覆盖带有提示的UINavigationBar,ios,uinavigationbar,uisearchbar,prompt,swift4,Ios,Uinavigationbar,Uisearchbar,Prompt,Swift4,我将UITableViewController与UISearchBar一起使用。UITableViewController嵌入在UINavigationController中。显示表格视图时,搜索栏按预期位于顶部(导航栏下方)。导航栏将显示标题和提示 单击搜索栏时,它会上移并在导航栏中隐藏标题。提示仍然可见。 当我不使用提示时,在单击搜索栏后,搜索栏仍在导航栏的下方 我创建了一个非常简单的项目来演示我的问题。项目具有UITableViewController的以下文件: import UIKit
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
}
}
在情节提要中,只有嵌入到UINavigationController中的空UITableViewController。结果如下所示
如果注释掉设置提示的行,则行为与预期一致
我最近将代码更新为Swift 4。我记不起我在使用Swift 3时看到过这种行为,但我不完全确定它是在Swift 4中引入的
我是在代码中遗漏了什么还是这是一个bug?在
viewDidLoad
中设置这些:
self.definesPresentationContext = true
self.edgesForExtendedLayout = .bottom