遇到坑了搔课。
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval
在這個過期方法中,調(diào)整橫豎屏 UICollectionView適配刹碾,其實就是間接調(diào)整Cell上下左右之間間距掏觉。在調(diào)試豎屏的時候死活只有一列繁堡。代碼如下,只是把interitemSpacing這個值改小了一點點口叙,猜想如果是正好的話系統(tǒng)計算的時候炼绘,會判斷超出范圍,只返回一列妄田。
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 這里要注意:由于是即將旋轉(zhuǎn)俺亮,最后的寬度就是現(xiàn)在的高度
// 總寬度
CGFloat totalWidth = self.view.height;
[self setupLayout:totalWidth orientation:toInterfaceOrientation];
}
/**
* 調(diào)整布局
*
* @param totalWidth 總寬度
* @param orientation 顯示的方向
*/
- (void)setupLayout:(CGFloat)totalWidth orientation:(UIInterfaceOrientation)orientation
{
// self.collectionViewLayout == self.collectionView.collectionViewLayout;
// 總列數(shù)
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;
// 設(shè)置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)