Merge pull request #500 from j-f1/scroll-discovery-to-top

Add support for scrolling the discovery tab to the top/first tab
This commit is contained in:
CMK 2022-11-08 15:00:52 +08:00 committed by GitHub
commit 9c9edcb717
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 1 deletions

View File

@ -14,6 +14,12 @@ protocol ScrollViewContainer: UIViewController {
extension ScrollViewContainer {
func scrollToTop(animated: Bool) {
scrollView.scrollRectToVisible(CGRect(origin: .zero, size: CGSize(width: 1, height: 1)), animated: animated)
scrollView.scrollToTop(animated: animated)
}
}
extension UIScrollView {
func scrollToTop(animated: Bool) {
scrollRectToVisible(CGRect(origin: .zero, size: CGSize(width: 1, height: 1)), animated: animated)
}
}

View File

@ -131,6 +131,13 @@ extension DiscoveryViewController: ScrollViewContainer {
var scrollView: UIScrollView {
return (currentViewController as? ScrollViewContainer)?.scrollView ?? UIScrollView()
}
func scrollToTop(animated: Bool) {
if scrollView.contentOffset.y <= 0 {
scrollToPage(.first, animated: animated)
} else {
scrollView.scrollToTop(animated: animated)
}
}
}
extension DiscoveryViewController {

View File

@ -190,6 +190,16 @@ extension SearchViewController: UISearchControllerDelegate {
}
}
// MARK: - ScrollViewContainer
extension SearchViewController: ScrollViewContainer {
var scrollView: UIScrollView {
discoveryViewController?.scrollView ?? UIScrollView()
}
func scrollToTop(animated: Bool) {
discoveryViewController?.scrollToTop(animated: animated)
}
}
// MARK: - UICollectionViewDelegate
//extension SearchViewController: UICollectionViewDelegate {
// func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {