【问题标题】:How make a collectionViewCell auto size only by height?如何仅按高度使 collectionViewCell 自动调整大小?
【发布时间】:2016-02-19 15:48:10
【问题描述】:

我使用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,我得到标签内容大小的单元格大小。

我的手机看起来像

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

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

【问题讨论】:

  • 我已经回答了,效果很好stackoverflow.com/a/31604233/3382056
  • Mihawk,这不是自动调整大小的示例。这个例子我也用过,但我想知道 autosize。
  • 此解决方案适用于自动布局

标签: ios autolayout uicollectionview uicollectionviewcell


【解决方案1】:

我找到了解决办法。

CollectionViewCell 有一个委托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。

【讨论】:

  • 拯救了我的一天!谢谢!
  • 你如何使用它?你在哪里设置委托?
【解决方案2】:

其实你在这里是约束优先级的问题。

要解决优先级约束问题,您应该以编程方式或直接从 InterFace Builder 更新 imageView Leading priority 999label Trailing 优先级 999

使用我实现的KVConstraintExtensionsMaster 以编程方式更新优先级,以更新没有自动布局约束IBOutlet reference 的任何约束。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell1" forIndexPath:indexPath];

    [cell.imageView changeAppliedConstraintPriority:999 forAttribute:NSLayoutAttributeLeading];
    [cell.label changeAppliedConstraintPriority:999 forAttribute:NSLayoutAttributeTrailing];

    return cell;
}

在这之后现在你需要计算单元格的唯一高度

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
     CGFloat width  = CGRectGetWidth(collectionView.bounds);
     CGFloat height  =  /* do here some calculation for height */
     return CGSizeMake(width, height);;
}

【讨论】:

  • U使用sizeForItemAtIndexPath,但是autoSize不用人工计算。
  • 当然对于宽度和高度都是 autoSize,但是您希望单元格将按 CollectionView 的全宽 (widthCell=widthCollectionView) 填充。所以这里的单元格宽度总是等于collectionView的宽度。
【解决方案3】:

Dmitry Nelepov在swift中的解决方案。

斯威夫特 4

override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
    let attr = super.preferredLayoutAttributesFitting(layoutAttributes)
    var frame = attr.frame
    frame.size.width = layoutAttributes.size.width //Key here
    attr.frame = frame
    return attr
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多