简体   繁体   English

按下“取消”按钮后,UISearchController取消VC

[英]UISearchController dismisses VC upon hitting cancel button

So, I am currently trying to replace the depricated searchDisplayController in one of my projects with UISearchController and I am running into this problem. 因此,我目前正在尝试用UISearchController替换我的一个项目中已描述的searchDisplayController,并且遇到了这个问题。

If there are no results in the search (the UITableView is empty) the whole ViewController is dismissed. 如果搜索中没有结果(UITableView为空),则整个ViewController将关闭。 This does not happen when the search results are not empty. 如果搜索结果不为空,则不会发生这种情况。 I wan't to make it clear I am not using a UITableViewController. 我不想弄清楚我没有使用UITableViewController。 Instead I have a regular VC with a UITableView in it. 相反,我有一个带有UITableView的常规VC。

Here is some of my code: 这是我的一些代码:

var resultSearchController = UISearchController()
override func viewDidLoad() {
    super.viewDidLoad()
    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.delegate = self
        controller.searchBar.delegate = self
        self.studentTable.tableHeaderView = controller.searchBar
        return controller
    })()
    ....
}

Now, if I add this function to the equation the cancel button always dismisses the VC. 现在,如果我将此函数添加到方程式中,则取消按钮始终关闭VC。

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    resultSearchController.active = false
}

So why exactly does setting the searchController.active = false dismiss the VC? 那么,为什么要完全将searchController.active = false设置为VC呢? Is it because it is using the same UITableView as the VC? 是因为它使用与VC相同的UITableView吗? I believe that the old searchDisplayController would just display a UITableView over the one being used. 我相信旧的searchDisplayController只会显示一个正在使用的UITableView。 If this is the case is there a way to override the dismissVC? 如果是这种情况,是否有一种方法可以覆盖dismissVC?

this is also Happening to me. 这也在我身上发生。 The Way I Solve it is by Replacing: 我解决问题的方法是更换:

   resultSearchController.active = false

with

    resultSearchController.searchBar.text = ""
    resultSearchController.searchBar.resignFirstResponder()

I Hope this helps you :-) 我希望这可以帮助你 :-)

2018 Just wanna share the fruits of my 1-2 hours debugging. 2018只是想分享我1-2小时调试的成果。

I had multiple issues with using UISearchController with UITabBarController , namely: 我在将UISearchControllerUITabBarController结合使用时遇到多个问题,即:

  1. This one, this very question of the OP. 这个,这是OP的问题。 Hitting cancel button dismisses the screen that is presenting the searchController. 点击取消按钮可关闭显示searchController的屏幕。

  2. The tab (or the screen) becomes black, Tab Bar and UISearchController giving black screen 选项卡(或屏幕)变为黑色, 选项卡栏和UISearchController变为黑色屏幕

  3. Using UISearchController inside the title view of the navigation bar of UINavigationController in both iOS 10, 11, and 12, like this questions. 在iOS 10、11和12中,都可以在UINavigationController导航栏的标题视图内使用UISearchController ,就像这样的问题。 UISearchBar increases navigation bar height in iOS 11 UISearchBar在iOS 11中增加导航栏的高度

And for the solution for #3, since we're already here: https://stackoverflow.com/a/53264329/3231194 对于#3的解决方案,因为我们已经在这里: https : //stackoverflow.com/a/53264329/3231194

Finally, the ONLY solution that I have been seeing all this time is adding this code: 最后,我一直看到的唯一解决方案是添加以下代码:

self.definesPresentationContext = true

The issue is that I was putting this in a wrong function. 问题是我将其置于错误的函数中。

Remember, that solution solved the #1, and #2 problem that I had. 请记住,该解决方案解决了我遇到的#1和#2问题。 Nothing more, nothing less. 仅此而已。

Where to add that? 在哪里添加? Inside the viewDidAppear . viewDidAppear内部。 That's it! 而已!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM