UICollectionView方向问题

时间:2013-06-04 16:08:52

标签: ios orientation uicollectionview

我目前有一个带有图像网格的集合视图,当选择一个图像时,它会转移到另一个带有全屏图像的集合视图。

为此我正在使用:

    - (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.

如何更正此问题,以便支持方向。

由于

1 个答案:

答案 0 :(得分:1)

您需要使集合视图的布局无效,因为纵向方向的单元格大小太大而无法横向显示。

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
  [self.collectionView.collectionViewLayout invalidateLayout];
}