我有一个标签栏有五个项目,我想添加一个功能,当用户再次点击标签栏项目时滚动到顶部。将UITabBarControllerDelegate
添加到我想要触发事件的视图中,并且还创建了一个函数来确定所选标签栏索引。Swift - 选择标签栏索引不触发滚动到顶部
当我打开应用程序时,索引0是自动选择的,并且工作完美。当我向下滚动并点击标签栏索引时,视图自动滚动到顶部。当我转到索引1并触发滚动时,会出现问题。它以某种方式完全删除我的第一个标签栏项的自动滚动。
选择没有自动滚动的其他标签栏项目根本不影响索引0。
主页(索引0)
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
if tabBarIndex == 0 {
self.collectionView?.setContentOffset(CGPoint(x: 0, y: -10), animated: true)
}
}
用户(索引1)
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
if tabBarIndex == 1 {
self.tableView?.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
}
}
甲'UITabBarController'只能有一个代表。最后一个胜利。 – rmaddy
真的!那么,如何将自动滚动添加到两个不同的标签栏项目呢? –