gpt4 book ai didi

ios - 如何使 collectionViewCell 仅按高度自动调整大小?

转载 作者:可可西里 更新时间:2023-11-01 03:06:52 25 4
gpt4 key购买 nike

我使用 collectionView 的自动调整流布局的功能

self.flow = [[UICollectionViewFlowLayout alloc] init];
self.flow.scrollDirection = UICollectionViewScrollDirectionVertical;
self.flow.sectionInset = UIEdgeInsetsZero;
self.flow.estimatedItemSize = CGSizeMake(320, 48);

我希望单元格将按 CollectionView 的全宽度 (widthCell=widthCollectionView) 填充

但我得到了整个尺寸的自动调整工作。我尝试使用内容压缩/拥抱优先级 - 无效。

CollectionView 的宽度等于 320px,我得到标签内容大小的单元格大小。

我的手机看起来像 enter image description here

水平约束:H:|-8-[img(48)]-8-[label]-8-|

如何使自动调整大小仅适用于垂直而不适用于水平?

最佳答案

我找到了解决方案。

CollectionViewCell 有一个委托(delegate) preferredLayoutAttributesFittingAttributes

因此,此函数为您提供 layoutAttributes 设置为 estimatedItemSize

你需要做下一步(高度)

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
UICollectionViewLayoutAttributes *attr = [super preferredLayoutAttributesFittingAttributes:layoutAttributes]; //Must be called
CGRect frame = attr.frame;
frame.size.width = layoutAttributes.size.width; //Key here
attr.frame = frame;
return attr;
}

同样,您可以更喜欢大小和宽度,因此使高度保持不变 (frame.size.height = layoutAttributes.size.height)。

如果只是复制 layoutAttributes 并更改宽度,不会有任何效果!

你必须调用 super。

关于ios - 如何使 collectionViewCell 仅按高度自动调整大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771075/

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