Answers:
我发现,滚动滚动viewWillAppear
可能无法可靠地进行,因为集合视图尚未完成其布局。您可能滚动到错误的项目。
我还发现,滚动viewDidAppear
将使未滚动视图的瞬间闪烁可见。
并且,如果您每次滚动viewDidLayoutSubviews
,用户将无法手动滚动,因为某些集合布局会在每次滚动时导致子视图布局。
这是我发现可靠工作的结果:
目标C:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// If we haven't done the initial scroll, do it once.
if (!self.initialScrollDone) {
self.initialScrollDone = YES;
[self.collectionView scrollToItemAtIndexPath:self.myInitialIndexPath
atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
}
}
斯威夫特:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if (!self.initialScrollDone) {
self.initialScrollDone = true
self.testNameCollectionView.scrollToItem(at:selectedIndexPath, at: .centeredHorizontally, animated: true)
}
}
[self.collectionView layoutIfNeeded];
,在内也可以使用viewDidLoad
。
新增,修改后的答案:
在此添加 viewDidLayoutSubviews
迅速
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let indexPath = IndexPath(item: 12, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: [.centeredVertically, .centeredHorizontally], animated: true)
}
通常.centerVertically
是这样
对象
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:12 inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically | UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
适用于旧iOS的旧答案
将此添加到viewWillAppear
:
[self.view layoutIfNeeded];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
indexPath
:[NSIndexPath indexPathForItem:numberOfTheObject inSection:sectionNumber]
我完全同意以上答案。唯一的问题是,对我来说,解决方案是在viewDidAppear中设置代码
viewDidAppear
{
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];
}
当我使用viewWillAppear时,滚动不起作用。
这似乎对我有用,它类似于另一个答案,但有一些明显的不同:
- (void)viewDidLayoutSubviews
{
[self.collectionView layoutIfNeeded];
NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:someInt inSection:currentItem.section];
[self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}
迅速:
your_CollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
迅捷3
let indexPath = IndexPath(row: itemIndex, section: sectionIndex)
collectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.right, animated: true)
滚动位置:
UICollectionViewScrollPosition.CenteredHorizontally / UICollectionViewScrollPosition.CenteredVertically
您可以使用GCD将滚动条分派到viewDidLoad中的主运行循环的下一个迭代中,以实现此行为。滚动将在屏幕上显示集合视图之前执行,因此不会闪烁。
- (void)viewDidLoad {
dispatch_async (dispatch_get_main_queue (), ^{
NSIndexPath *indexPath = YOUR_DESIRED_INDEXPATH;
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
});
}
我建议在中进行操作。collectionView: willDisplay:
然后,您可以确保集合视图委托提供了某些东西。这是我的例子:
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
/// this is done to set the index on start to 1 to have at index 0 the last image to have a infinity loop
if !didScrollToSecondIndex {
self.scrollToItem(at: IndexPath(row: 1, section: 0), at: .left, animated: false)
didScrollToSecondIndex = true
}
}
所有答案都在这里-骇客。我发现了在relaodData之后将集合视图滚动到某个位置的更好方法:
子类集合视图布局使用的布局,重写方法prepareLayout,在超级调用后设置所需的偏移量。
例如:https://stackoverflow.com/a/34192787/1400119
initialScrollDone
LenK所做的标记,因为此方法将被多次调用,并且如果您调用scrollToItemAtIndexPath:不止一次,它似乎会使collectionView不可滚动(iOS模拟器iPhone 5 / iOS 8)