gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:36:46 25 4
gpt4 key购买 nike

纵向显示,我所有的CollectionViewCells都正确对齐。

enter image description here

当切换到横向时,单元格现在间隔更大。

enter image description here

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

最佳答案

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

-(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;
}
}

关于ios - 如何在方向更改时重新计算UICollectionViewFlowLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41389634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com