1

I currently have a collection view with a grid of images on it, when selecting an image this segues to another collection view with a full screen image on it.

为此,我正在使用:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"collectionView"])

    {
        QuartzDetailViewController *destViewController = (QuartzDetailViewController *)segue.destinationViewController;

        NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0];

        destViewController.startingIndexPath = indexPath;

        [destViewController.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];

        [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
    }
}

然后在我的详细视图中:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (_startingIndexPath) {
    NSInteger currentIndex = floor((scrollView.contentOffset.x - scrollView.bounds.size.width / 2) / scrollView.bounds.size.width) + 1;
    if (currentIndex < [self.quartzImages count]) {
        self.title = self.quartzImages[currentIndex][@"name"];
    }
}
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    layout.itemSize = self.view.bounds.size;

    [self.collectionView scrollToItemAtIndexPath:self.startingIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}

当我旋转到横向时,图像消失并且顶部的标题发生变化,如果我返回网格然后再次选择一个单元格,它将再次进入详细视图,但标题是不同的单元格并且图像显示一半和两个不同图像之间的一半。

旋转设备时,我也在日志中收到以下消息:

2013-06-04 17:39:53.869 PhotoApp[6866:907] the behavior of the UICollectionViewFlowLayout is not defined because:
2013-06-04 17:39:53.877 PhotoApp[6866:907] the item height must be less that the height of the UICollectionView minus the section insets top and bottom values.

我该如何纠正这个问题,以便它支持方向。

谢谢

4

1 回答 1

1

您需要使集合视图的布局无效,因为纵向的单元格尺寸对于横向来说太大了。

在你的父 UIViewController 中写这样的东西应该可以解决你的问题:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
  [self.collectionView.collectionViewLayout invalidateLayout];
} 
于 2013-07-20T21:17:24.023 回答