当按下相应视图控制器的 tabBar 图标时,如何将 collectionView 带回顶部?

How can I bring a collectionView back to the top when the tabBar icon for that respective view controller is pressed?

我正在尝试在 swift 中构建一个社交媒体应用程序,它有一个带有 4 个图标的选项卡栏,每个图标都连接到一个视图控制器。我想让四个集合视图(每个都与一个视图控制器相关联)在按下相应的选项卡栏图标时滚动回顶部(就像推特一样)。

只有当过去的标签栏图标用于相应的集合视图时,我才知道如何让集合视图返回顶部的逻辑(这样它就不会自动滚动到顶部当单击相应的选项卡但需要再次按下时)我尝试调试我的第一个开关案例但没有运气所以我还没有其他案例的完整代码。我尝试做的是引用正确的故事板,然后是正确的视图控制器(在本例中为 homeViewController),最后调用相应的集合视图并使用 setContentOffset 将其置于顶部。

下面我提供我的全部tabBarController class.

import Foundation
import UIKit

class MainTabController: UITabBarController, UITabBarControllerDelegate {

    var pastTabBar: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self


    }


    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

            let tabBarIndex = tabBar.items?.index(of: item)

            if tabBarIndex == pastTabBar {

                switch tabBarIndex {

                case 0:
                    print ("go to top of home")
                    //updateHomeCollection = 1
                    let HomeSB : UIStoryboard = UIStoryboard(name: "Home", bundle: nil)
                    let HomeVC = HomeSB.instantiateViewController(withIdentifier: "Home") as? HomeViewController
                    HomeVC?.collectionView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
                case 1:
                    print ("go to top of search")
                case 2:
                    print ("go to top of notifications")

                case 3:
                    print ("go to top of profile")

                default:
                    print ("not working")
                }
            }

        if let tabBarIndex = tabBarIndex {
            pastTabBar = tabBarIndex
        }

        }

}

当我点击主页标签时,它一直在中止信号!它正在打印这个确切的声明:

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

在这行代码中:

HomeVC?.collectionView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)

加载 ViewController 不是最好的方法。您必须从 Navigation 中获取所需的 ViewController,如下所示

extension HomeTabVC: UITabBarControllerDelegate {

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if previousController == viewController {
            if let nav = viewController as? UINavigationController, let vc = nav.viewControllers[0] as? FeaturedVC {
                if vc.isViewLoaded && (vc.view.window != nil) {
                    let visibleIndex = vc.collectionFeatured.indexPathsForVisibleItems
                    if visibleIndex.count != 0 {
                        vc.collectionFeatured.scrollToItem(at: IndexPath (item: 0, section: 0), at: .bottom, animated: true)
                    }
                }
            }else if let nav = viewController as? UINavigationController, let vc = nav.viewControllers[0] as? CategoryVC {
                if vc.isViewLoaded && (vc.view.window != nil) {
                    let visibleIndex = vc.collectionViewCategory.indexPathsForVisibleItems
                    if visibleIndex.count != 0 {
                        vc.collectionViewCategory.scrollToItem(at: IndexPath (item: 0, section: 0), at: .bottom, animated: true)
                    }
                }
            }else{

            }
        }

        previousController = viewController

    }
}

您必须将 Selected ViewController 存储在变量中,这样当您将 ViewController 从一个选项卡更改为另一个选项卡时,它只会更改 ViewController,当您再次点击同一个选项卡时,它会滚动你回到顶部。这就是 iOS 在所有应用程序中自我完成的方式