我在使用 UISearchController 时遇到问题,如果我在搜索栏中有文本并关闭它所在的 VC,则 searchBar 不会消失,只会保留在屏幕上,与其他 VC 中的所有内容重叠。如果您点击取消按钮,它就会崩溃。
已经尝试了一些解决方案,但没有一个有效。 :/
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
})()
非常感谢对此的任何帮助!
更新:所以我找到了一个不太好的解决方案,即在 viewWillDisappear 中设置 .active = false 。然而,问题是 searchBar 工件会在下一个/上一个 VC 上显示一秒钟,然后完全消失。
更改以下内容:
controller.definesPresentationContext = true
to
self.definesPresentationContext = true
当您在导航堆栈上推送和弹出视图控制器时,该设置还会保留搜索栏的状态。
您可以阅读有关definesPresentationContext
设置为Apple 关于 UIViewController 的文档 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/occ/instp/UIViewController/definesPresentationContext.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)