UISearchController 不会关闭 iOS 8 Swift 的搜索栏和重叠 [英] UISearchController won't dismiss searchbar and overlap for iOS 8 Swift

查看:31
本文介绍了UISearchController 不会关闭 iOS 8 Swift 的搜索栏和重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 UISearchController 时遇到问题,如果我在搜索栏中有文本并关闭它所在的 VC,则搜索栏不会消失,只会保留在屏幕上与其他 VC 中的所有内容重叠.然后,如果您点击取消按钮,它就会崩溃.

I'm having problem with the UISearchController where if I have text in the searchbar and dismiss the VC it's in, the searchBar won't go away and just remain on screen overlapping everything in other VCs. Then it crashes if you hit the cancel button.

在 SO 上尝试了一些解决方案,但没有一个奏效.:/

Have tried a few solutions on SO but none have worked. :/

self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.hidesNavigationBarDuringPresentation = false
        controller.searchBar.sizeToFit()

        controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
        controller.searchBar.barTintColor = UIColor(red: 243/255, green: 243/255, blue: 243/255, alpha: 1)
        controller.searchBar.tintColor = UIColor.blackColor()

        controller.definesPresentationContext = true
        controller.edgesForExtendedLayout = UIRectEdge.None
        self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0)

        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

非常感谢您对此的任何帮助!

Really appreciate any help on this!

更新:所以我找到了一个不太好的解决方案,即在 viewWillDisappear 中设置 .active = false.但是,问题是 searchBar 工件会在下一个/上一个 VC 上显示一秒钟,然后完全消失.

UPDATE: So I kinda found a not-so-great solution which is to set .active = false in viewWillDisappear. However, the problem is there a searchBar artifact will show on the next/previous VC for a second before completely disappearing.

推荐答案

更改以下内容:

controller.definesPresentationContext = true

self.definesPresentationContext = true

当您在导航堆栈上推入和弹出视图控制器时,该设置还会保留搜索栏的状态.

The setting also preserves the state of the search bar when you push and pop a view controller on and off the navigation stack.

您可以在 苹果关于 UIViewController 的文档.

You can read about the definesPresentationContext setting on Apple's Documentation on UIViewController.

这篇关于UISearchController 不会关闭 iOS 8 Swift 的搜索栏和重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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