我试图这样做,当用户第二次点击标签栏项时,它们将自动滚动到当前屏幕的顶部。
我有一个自定义标签栏视图,其中包含以下代码,可在视图之间切换:
- (IBAction)onTabSelectionChange:(id)sender {
UIButton *btn = (UIButton*)sender;
previousIndex = currentIndex;
currentIndex = btn.tag;
if (btn.tag == previousIndex){
// This is where the code should probably go?
} else {
if (previousIndex != 0){
UIButton *btnpreviousIndex = (UIButton*)[tabView viewWithTag:previousIndex];
if (btnpreviousIndex != nil && previousIndex != -1){
[btnpreviousIndex setSelected:NO];
}
[btn setSelected:YES];
}
}
appDelegate.currentTab = currentIndex;
switch (currentIndex) {
case TABHOME: {
UINavigationController *navController = [appDelegate.arrViewControllers objectAtIndex:0];
navController.navigationBarHidden = YES;
[navController popToRootViewControllerAnimated:NO];
[self presentThisView: navController];
}
break;
case TABTIMELINE: {
UINavigationController *navController = [appDelegate.arrViewControllers objectAtIndex:1];
[self presentThisView: navController];
}
break;
case TABNOTIFICATION: {
UINavigationController *navController = [appDelegate.arrViewControllers objectAtIndex:2];
[self presentThisView: navController];
break;
}
case TABMISCELLANEOUS: {
UINavigationController *navController = [appDelegate.arrViewControllers objectAtIndex:3];
[self presentThisView: navController];
break;
}
default:
break;
}
}
提前感谢您的帮助!
答案 0 :(得分:1)
如果它是滚动视图,那么您可以将此代码放在您指定的位置:
[scrollView setContentOffset:CGPointZero animated:YES];
<强>更新强>
您需要在当前选项卡索引处访问视图控制器,然后您可以在其中获取collectionView并执行:
[self.collectionView setContentOffset:CGPointMake(0.0f,0.0f)];
你真的应该实现UITabBarControllerDelegate
并将当前代码放在方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController*)viewController {
通过这种方式,您可以了解前一个和所选视图控制器的内容,然后也可以像我建议的那样访问内部的集合视图。
答案 1 :(得分:0)
您可以使用此方法滚动到所需的任何collectionview项目
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath
atScrollPosition:(UICollectionViewScrollPosition)scrollPosition
animated:(BOOL)animated
或者您也可以使用[collectionView setContentOffset:CGPointZero animated:YES];
如果你想回到收藏的最开头,你可以简单地使用CGPointZero