scrollToItemAtIndexPath无法正常工作
问题描述:
我试图使用UICollectionView
实现照片库。该设置与此tutorial中的设置类似:单元格与收藏视图一样大,因此您一次只能看到一张图片。分页已启用,因此您可以逐张浏览图片。目前为止一切正常。scrollToItemAtIndexPath无法正常工作
我还想在设备旋转到横向时保持该设置。它在细胞/图像大小方面工作良好。但是像上面教程中所描述的那样,收藏视图会旋转到两张图片之间的某个奇怪位置。
我的目标是让旋转后显示旋转后的同一单元格的集合视图。就像在这post。
我试图解决这个问题:
旋转之前,我目前可见项目的indexpath
保存财产,像这样:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSArray *visibleItems = [self.galleryCollectionView indexPathsForVisibleItems];
self.currentIndexPath = [visibleItems lastObject];
[self.galleryCollectionView.collectionViewLayout invalidateLayout];
}
和旋转后,我试图滚动到该项目如下:
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self.galleryCollectionView scrollToItemAtIndexPath:self.currentIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
不幸的是,这只是为前两项中的收集,如果我滚动让我们说第五个项目,并旋转设备它旋转到一些奇怪的细胞之间的位置再次。
任何想法我做错了什么?
答
我在iOS 6完全一样的问题,它是固定在iOS 7,下面是对我的作品的iOS 6
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
// workaround iOS 6 collection view rotation bug: UICollectionView is not scrolled to the correct index after rotation
[self.collectionView setContentOffset:[self.collectionView.collectionViewLayout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:self.currentPage inSection:0]].frame.origin animated:NO];
}
我没有一个想法是什么不正常的解决方法对你来说,对我来说它似乎应该起作用,但是我想推荐你使用一个现成的库来显示一个照片库,因为它有这样一个常见的任务,并且有这么好的代码,并且根据需要折射它。我已经完成了许多项目,包括一个画廊,并且对人们经常使用的“经过强化的”代码进行重构通常是时间有效的,例如MWPhotoBrowser –
您是否找到了解决问题的方法? –
我在iOS 6上有完全相同的问题,但它已在iOS 7中修复。 –