我在 View Controller 中配置了一个搜索 Controller :
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = getLocalizedString("searchByName")
searchController.searchBar.setValue(getLocalizedString("cancel"), forKey:"_cancelButtonText")
searchController.searchBar.delegate = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.showsCancelButton = true
searchController.searchBar.sizeToFit()
然后,当单击导航栏中的搜索按钮时,我会在导航栏的 titleView 中显示搜索栏:
self.navigationItem.setHidesBackButton(true, animated: true)
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.titleView = searchController.searchBar
searchController.searchBar.becomeFirstResponder()
当我将此 View Controller 用作选项卡栏项时,搜索栏按预期工作。
我还将 View Controller 与另一个 View Controller 的 segue 重用,该 View Controller 也有一个搜索 Controller 。
执行 segue 后,我单击搜索按钮,出现了搜索栏,但没有响应点击或任何内容。
更新
我刚刚发现这是因为我正在使用:
self.definesPresentationContext = true
知道为什么吗?我需要它,因为如果不这样做,如果我在标签之间移动,屏幕就会变黑。
谢谢。
最佳答案
对于寻找答案的人,我可以通过在 prepareForSegue
方法中将 definesPresentationContext
设置为 false 来修复它。
另外,为了避免用户返回时出现黑屏,我还在 viewWillAppear
中将其设置回 true。
关于swift - 重新使用的 ViewController 中的搜索栏在 segue 后没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36617376/