首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >旋转时UICollectionView不适配iPad屏幕

旋转时UICollectionView不适配iPad屏幕
EN

Stack Overflow用户
提问于 2014-03-24 23:25:32
回答 1查看 1.4K关注 0票数 1
代码语言:javascript
复制
#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];
}];


}

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

EN

回答 1

Stack Overflow用户

发布于 2014-04-04 22:27:44

您在何处设置collectionView的新帧?您可以在willAnimateRotationToInterfaceOrientation: duration:didRotateFromInterfaceOrientation:中设置它,或者如果它等于superview的大小,则将大小调整蒙版设置为灵活的宽度或高度。

代码语言:javascript
复制
_collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

viewWillLayoutSubviews中,您将itemSize设置为横向和纵向相同,这是有意的吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22613607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档