我在导航 Controller 中嵌入了 View Controller (BlueViewController)。
blueViewController 有一个 UISearchController,设置为 blueViewController 的 tableView headerView。
searchController 工作正常,除了当它处于事件状态时,它会向上移动并隐藏在 UINavigationController 后面(或某处)
我尝试将搜索 Controller 的 searchBar 作为 subview 添加到 viewController,或者定位 UIView 并将 searchBar 分配给该 UIView,但这些都不起作用
我已经尝试过此链接,但不起作用 Strange UISearchDisplayController view offset behavior in iOS 7 when embedded in navigation bar
这里是BlueViewController中的searchController
let searchController:UISearchController = {
let controller = UISearchController(searchResultsController: nil)
controller.hidesNavigationBarDuringPresentation = false
controller.obscuresBackgroundDuringPresentation = false
controller.searchBar.inputAssistantItem.leadingBarButtonGroups = []
controller.searchBar.inputAssistantItem.trailingBarButtonGroups = []
return controller
}()
在viewDidLoad中
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
definesPresentationContext = true
您还可以在这里找到整个项目 https://github.com/QiquanLu/TestNavigationWithSearchController
任何提示将不胜感激,谢谢!
最佳答案
对于 iOS 11 及更高版本,您应该在 BlueViewController
的 navigationItem 上设置 searchController
属性,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.searchController = searchController
}
不要将其添加为 tableView 的标题。
关于swift - 在 UINavigationController 下输入时 UISearchController 会向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55832321/