如何在方向更改时重新计算UICollectionViewFlowLayout?

时间:2016-12-30 01:55:55

标签: ios uicollectionview

在纵向中,我的所有CollectionViewCell都正确对齐。

enter image description here

当切换到横向时,细胞现在间隔更多。

enter image description here

在方向更改中重新计算UICollectionViewFlowLayout's minimumInterimSpacingitemSize的正确方法是什么?我希望在横向上显示6列。

2 个答案:

答案 0 :(得分:4)

由于Syed e Hussaini已经给出了正确答案,我将把我的遗留给Swift中需要它的人。

您需要在视图控制器中符合UICollectionViewDelegateFlowLayout,然后使用其方法sizeForItemAt()minimumInteritemSpacingForSectionAt()来确定大小和间距,然后使用viewWillTransitionToSize()来检测方向更改。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    // Reloads collection view
    self.collectionView.reloadData()
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    if UIDevice.current.orientation.isLandscape {
        // Returns size for cell when in landscape
    } else {
        // Returns size for cell when NOT in landscape
    }
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {

    if UIDevice.current.orientation.isLandscape {
        // Returns spacing for cells when in landscape
    } else {
        // Returns spacing for cells when NOT in landscape
    }
}

答案 1 :(得分:2)

使用此逻辑可根据设备方向更改单元格大小和间距。

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    //create two properties for storing a bool and sizeOfYourScreen
    //In your viewDidLoad method initialize both of these object with NO and self.view.frame.size
    //get your Portrait screen size
    if (portraitScreenSize > size.height) {
        self.isPortrate = NO;
    }else{
        self.isPortrate = YES;
    }
    self.size = size;
    [self.collectionView reloadData];
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (self.isPortrate) {

        //Calculate your interitemSpacing with respect to size and then simply return it
        return newInteritemSpacing;
    }else{
        //Calculate your interitemSpacing with respect to size and then simply return it
        return newInteritemSpacing;
    }

}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (self.isPortrate) {

        //Calculate your lineSpacing with respect to size and then simply return it
        return newlineSpacing;
    }else{
        //Calculate your interitemSpacing with respect to size and then simply return it
        return newlineSpacing;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //Here you should return your size of each item
    if (self.isPortrate) {

        //Calculate your lineSpacing with respect to size and then simply return it
        return sizeOfCellInPortrate;
    }else{
        //Calculate your interitemSpacing with respect to size and then simply return it
        return sizeOfCellInLandscape;
    }
}