gpt4 book ai didi

ios - 当按下相应 View Controller 的 tabBar 图标时,如何将 collectionView 带回顶部?

转载 作者:行者123 更新时间:2023-11-28 07:25:55 26 4
gpt4 key购买 nike

我正在尝试快速构建一个社交媒体应用程序,它有一个标签栏,其中有 4 个图标,每个图标都连接到一个 View Controller 。我想让四个 Collection View (每个都与一个 View Controller 相关联)在按下相应的选项卡栏图标时滚动回顶部(就像推特一样)。

只有当过去的标签栏图标用于相应的 Collection View 时,我才希望 Collection View 返回顶部的逻辑(这样它不会在单击时自动滚动到顶部在各自的选项卡上,但需要再次按下)我试图调试我的第一个开关案例,但没有运气,所以我还没有其他案例的完整代码。我尝试做的是引用正确的 Storyboard,然后是正确的 View Controller (在本例中为 homeViewController),最后调用相应的 Collection View 并使用 setContentOffset 来实现它到顶部。

下面我提供了我的整个 tabBarController 类。

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 在所有应用程序中 self 完成的方式

关于ios - 当按下相应 View Controller 的 tabBar 图标时,如何将 collectionView 带回顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56607180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com