我目前有以下代码片段来计算UICollectionViewCells大小:
- (CGSize)collectionView:(UICollectionView *)mainCollectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)atIndexPath { CGSize bounds = mainCollectionView.bounds.size; bounds.height /= 4; bounds.width /= 4; return bounds; }
这个工作.然而,我现在在viewDidLoad中添加一个键盘观察器(它触发了UICollectionView的委托和数据源方法,然后再出现,并从故事板中调整大小).这样的界限是错误的.我也想支持轮换.如果UICollectionView改变大小,那么处理这两个边缘情况并重新计算大小的好方法是什么?
解决方法
当集合视图的边界更改时,无效布局的解决方案是覆盖shouldInvalidateLayoutForBoundsChange:并返回YES.
在文档中也有说明: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionViewLayout_class/index.html#//apple_ref/occ/instm/UICollectionViewLayout/shouldInvalidateLayoutForBoundsChange:
在文档中也有说明: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UICollectionViewLayout_class/index.html#//apple_ref/occ/instm/UICollectionViewLayout/shouldInvalidateLayoutForBoundsChange:
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; }
这也应该涵盖旋转支持.如果没有,实现viewWillTransitionToSize:withTransitionCoordinator:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { [self.collectionView.collectionViewLayout invalidateLayout]; } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { }]; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。