旋转期间UICollectionView无法适应iPad屏幕

时间:2014-03-24 15:25:32

标签: uicollectionview uiinterfaceorientation screen-rotation xcode5.1

#pragma mark Rotation handling methods

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:
(NSTimeInterval)duration {
NSLog(@"IS ROTATING");
// Fade the collectionView out
[self.collectionView setAlpha:0.0f];

// Suppress the layout errors by invalidating the layout
[self.collectionView.collectionViewLayout invalidateLayout];

// Calculate the index of the item that the collectionView is currently displaying
CGPoint currentOffset = [self.collectionView contentOffset];
self.currentIndex = currentOffset.x / self.collectionView.frame.size.width;
}

- (void)viewWillLayoutSubviews;
{
[super viewWillLayoutSubviews];
UICollectionViewFlowLayout *flowLayout = (id)self.collectionView.collectionViewLayout;

if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
    flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
} else {
    flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
}

[flowLayout invalidateLayout]; //force the elements to get laid out again with the new size
}


-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {

// Force realignment of cell being displayed
CGSize currentSize = self.view.bounds.size;
float offset = self.currentIndex * currentSize.width;
[self.collectionView setContentOffset:CGPointMake(offset, 0)];


NSLog(@"CURRENT bounds: %f",self.view.bounds.size.width);
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.currentIndex inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];


// Fade the collectionView back in
[UIView animateWithDuration:0.125f animations:^{
    [self.collectionView setAlpha:1.0f];
}];


}

这是代码。我只是希望图像保持全屏,并且集合视图符合肖像。我可以看到单元格正在尝试调整,但集合视图本身并没有调整大小。有什么建议/修正吗?

1 个答案:

答案 0 :(得分:1)

您在哪里为collectionView设置新框架?您应该在willAnimateRotationToInterfaceOrientation: duration:didRotateFromInterfaceOrientation:中设置它,或者如果它等于超视图的大小,则将调整大小蒙版设置为灵活的宽度或高度。

_collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

viewWillLayoutSubviews中,您要设置横向和纵向的itemSize,是否有意?