UISearchController 搜索栏与 CollectionView 重叠
UISearchController searchbar overlaps CollectionView
以编程方式启动 UISearchController 时,UISearchBar 与下方的 CollectionView 重叠。
我到处搜索了一下,好像以前没有人遇到过这种问题。
我能做什么和不能做什么有一些限制:
- 无法使用表视图header
- 无法使用集合视图header
- 必须使用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)
在使用 Cancel 按钮关闭 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,所以我必须do 将其设置为 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 时,UISearchBar 与下方的 CollectionView 重叠。
我到处搜索了一下,好像以前没有人遇到过这种问题。
我能做什么和不能做什么有一些限制:
- 无法使用表视图header
- 无法使用集合视图header
- 必须使用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)
在使用 Cancel 按钮关闭 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,所以我必须do 将其设置为 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)
}