遇到坑了。
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval
在这个过期方法中,调整横竖屏 UICollectionView适配,其实就是间接调整Cell上下左右之间间距。在调试竖屏的时候死活只有一列。代码如下,只是把interitemSpacing这个值改小了一点点,猜想如果是正好的话系统计算的时候,会判断超出范围,只返回一列。
CGFloat interitemSpacing = (totalWidth - columns * layout.itemSize.width) / (columns + 1) / 1.1;
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self setupLayout:self.view.width orientation:self.interfaceOrientation];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
#warning 这里要注意:由于是即将旋转,最后的宽度就是现在的高度
// 总宽度
CGFloat totalWidth = self.view.height;
[self setupLayout:totalWidth orientation:toInterfaceOrientation];
}
/**
* 调整布局
*
* @param totalWidth 总宽度
* @param orientation 显示的方向
*/
- (void)setupLayout:(CGFloat)totalWidth orientation:(UIInterfaceOrientation)orientation
{
// self.collectionViewLayout == self.collectionView.collectionViewLayout;
// 总列数
int columns = UIInterfaceOrientationIsPortrait(orientation) ? 2 : 3;
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
// 每一行的最小间距
CGFloat lineSpacing = 25;
// 每一列的最小间距
CGFloat interitemSpacing = (totalWidth - columns * layout.itemSize.width) / (columns + 1) / 1.1;
layout.minimumInteritemSpacing = interitemSpacing;
layout.minimumLineSpacing = lineSpacing;
// 设置cell与CollectionView边缘的间距
layout.sectionInset = UIEdgeInsetsMake(lineSpacing, interitemSpacing, lineSpacing, interitemSpacing);
}```
图片如下:
![屏幕快照 2016-04-19 上午12.21.08.png](http://upload-images.jianshu.io/upload_images/1433768-367efb03ff1b2723.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)