UISearchController在segue之后仍然存在 [英] UISearchController persisting after segue

查看:156
本文介绍了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 。在 prepareForSegue()中添加以下代码

searchController.active = false


$ b < c $ c>获取默认行为

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

definesPresentationContext = true

definesPresentationContext


一个布尔值,指示是否覆盖此视图控制器的视图

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.

如果视图控制器返回true,然后它提供一个演示
上下文。由视图控制器
视图覆盖的窗口部分确定了所呈现的视图控制器视图的大小。
此属性的默认值为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.

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

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