UISearchController搜索栏重叠CollectionView
以编程方式启动UISearchController时,UISearchBar与下面的CollectionView重叠。UISearchController搜索栏重叠CollectionView
我在这里和那里搜索,似乎没有人有过这种问题。
有关于如何我能做和不能做一些限制:
- 不能使用的tableview头
- 不能使用的CollectionView头
- 必须使用collectionviewcontroller
已经尝试调整滚动视图插入和扩展边缘 - 没有用,因为它的一个CollectionViewController
问题是:如何以正确的方式使用IBAction? 连接到放大镜IBAction为代码是:
@IBAction func showSearchBar(_ sender: UIBarButtonItem) {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
present(searchController, animated: true, completion: nil)
}
感谢@noir_eagle建议,我已经实现竣工块present(searchController, animated: true, completion: nil)
方法,所以它看起来是这样的:
present(searchController, animated: true, completion: {
UIView.animate(withDuration: 0.25, animations: {
self.collectionView?.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)
self.collectionView?.contentOffset = CGPoint(x: 0, y: -64)
})
})
我不得不这样做,因为UISearchControllerDelegate
方法didPresentSearchController(_ searchController: UISearchController)
和willPresentSearchController(_ searchController: UISearchController)
永远不会被称为...不知道为什么。
接下来,不知何故,方法UISearchControllerDelegate
didDismissSearchController(_ searchController: UISearchController)
被解雇UISearchBar
与取消按钮后立即调用,所以我实现了这一点:
func didDismissSearchController(_ searchController: UISearchController) {
UIView.animate(withDuration: 0.25, animations: {
self.collectionView?.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0)
})
}
而且这是很好的......流畅的动画效果,直到我已经意识到searchController.hidesNavigationBarDuringPresentation
的属性是true,所以我所要做的就是将其设置为false。
就是这样,不需要contentInset
或contentOffset
动画! 最终的解决方案看起来像这样:
@IBAction func showSearchBar(_ sender: UIBarButtonItem) {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
present(searchController, animated: true, completion: nil)
}
正如我记得UISearchController有一个委托方法,就像成为一个活动。你有没有试图改变该方法中的collectionView的insets? –
这看起来并不正确......你怎么有两个状态栏?你能否创建一个能够再现这个结果的小型项目? – DonMag
这些不是状态栏,它是顶部栏。当UISearchController被调用时,如上面的代码所示,它将标准顶部栏替换为UISearchBar界面。一旦UISearchBar被** Cancel **按钮解除,标准的顶部栏返回。 – gutaker