gpt4 book ai didi

ios - 如何根据设备屏幕大小调整 Collection View 单元格的大小?

转载 作者:IT王子 更新时间:2023-10-29 08:16:49 24 4
gpt4 key购买 nike

我注意到单元格将始终遵循尺寸检查器中的定义。即使我已经应用了 UICollectionViewDelegateFlowLayout。

enter image description here

enter image description here

这就是它的样子。但我希望这些单元格看起来更像是在较小的 iPhone 屏幕上的样子。

enter image description here

最佳答案

根据 View 的帧大小实现sizeForItemAt方法

swift

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

let height = view.frame.size.height
let width = view.frame.size.width
// in case you you want the cell to be 40% of your controllers view
return CGSize(width: width * 0.4, height: height * 0.4)
}

objective-c

- (CGSize)collectionView:(UICollectionView *)collectionView 
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

CGFloat height = self.view.frame.size.height;
CGFloat width = self.view.frame.size.width;
// in case you you want the cell to be 40% of your controllers view
return CGSizeMake(width * 0.4, height * 0.4);
}

关于ios - 如何根据设备屏幕大小调整 Collection View 单元格的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40974973/

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