UISearchController 在 segue 后持续存在 [英] UISearchController persisting after segue

查看:29
本文介绍了UISearchController 在 segue 后持续存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 UISearchController 的应用程序.UI 的这个元素完全用这样的代码设置:

I have an app with a UISearchController. This element of the UI is completely set up in code like this:

searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal

searchController.searchBar.frame = CGRectMake(searchController.searchBar.frame.origin.x, searchController.searchBar.frame.origin.y, searchController.searchBar.frame.size.width, 44.0)

然后我将它添加到我的 tableView 的 tableHeaderView

I am then adding it to my tableView's tableHeaderView

tableView.tableHeaderView = searchController.searchBar

似乎一切正常,但是当它处于活动状态并且我在我的 tableView 中选择一个项目时,我的应用程序会转到另一个视图控制器,而搜索控制器则保留在视图中.我不确定这是怎么可能的,因为搜索控制器应该是另一个视图控制器中表视图的子视图.我怎样才能防止这种情况发生?

Everything seems to be working fine, but when it's active and I select an item in my tableView, my app segues to another view controller with the search controller persisting in the view. I'm unsure as to how this is possible since the search controller should be a subview of the table view in another view controller. How can I prevent this from happening?

推荐答案

您可以通过在 prepareForSegue 中将 active 属性设置为 false 来手动隐藏 searchController.在 prepareForSegue()

You can hide the searchController manually by setting the active property to false in prepareForSegue. Add the below code in prepareForSegue()

searchController.active = false

或者,您应该在 viewDidLoad() 中添加以下行以获得默认行为

Alternatively, you should add the following line in viewDidLoad() to get the default behaviour

definesPresentationContext = true

来自 定义PresentationContext

一个布尔值,指示当视图控制器或其后代之一呈现视图控制器时是否覆盖此视图控制器的视图.

A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.

讨论

当一个视图控制器被呈现时,iOS从呈现的视图控制器开始并询问它是否要提供呈现语境.如果呈现视图控制器不提供上下文,然后 iOS 询问呈现视图控制器的父视图控制器.iOS 在视图控制器层次结构中向上搜索直到视图控制器提供表示上下文.如果没有视图控制器提供了一个上下文,即窗口的根视图控制器提供表示上下文.

When a view controller is presented, iOS starts with the presenting view controller and asks it if it wants to provide the presentation context. If the presenting view controller does not provide a context, then iOS asks the presenting view controller's parent view controller. iOS searches up through the view controller hierarchy until a view controller provides a presentation context. If no view controller offers to provide a context, the window's root view controller provides the presentation context.

如果一个视图控制器返回真,那么它提供一个演示语境.视图控制器覆盖的窗口部分view 确定呈现的视图控制器视图的大小.此属性的默认值为 false.

If a view controller returns true, then it provides a presentation context. The portion of the window covered by the view controller's view determines the size of the presented view controller's view. The default value for this property is false.

重要说明(来自评论中的@paulvs)

Important note (from @paulvs in the comments)

小问题.在视图控制器上设置definePresentationContext,而不是搜索控制器,我觉得这点值得强调一下.

Little gotcha. Set definesPresentationContext on the view controller, not the search controller, I think this is worth emphasising.

这篇关于UISearchController 在 segue 后持续存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆