swift - 重新使用的 ViewController 中的搜索栏在 segue 后没有响应

标签 swift ios9 uisearchcontroller searchbar

我在 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/

相关文章:

swift - 在某些情况下,使用 searchController 推送过渡不会使 navigationBar 的大小变化平滑

ios - 我如何将 SFSafariViewController 与这个用户一样的深色主题一起使用?

authentication - iOS - Facebook SDK v3.24.1 登录屏幕为空白,使用 FBSessionLoginBehaviorForcingWebView 时除外

ios - 在 iOS 9 中,为什么 SFSafariViewController 被推送而不是模态呈现?

ios - 如何使用uisearchcontroller在ios 9中添加tableview的搜索选项,与objectiveC

ios - 如何使用 Firebase 进行多次观察?

ios - 在同一个 View Controller 上按下 AlertViewController 上的确定按钮后重新加载 Collection View ?

swift - 如何向基于文档的 Swift 3 应用程序添加简单的保存加载功能?

ios - MapView 注释中的较长字幕(快速)

ios - 使用 UISearchController 以编程方式定位 UISearchBar