UISearchController persisting after segue

前端 未结 2 2058
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 06:55

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

searchController = UISearchController(searchResultsCo         


        
2条回答
  •  有刺的猬
    2020-12-03 07:30

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

    searchController.active = false
    

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

    definesPresentationContext = true
    

    From the documentation for 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.

    Discussion

    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.

    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.

    Important note (from @paulvs in the comments)

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

提交回复
热议问题