How make a collectionViewCell auto size only by height?

喜夏-厌秋 提交于 2019-12-04 03:15:05
Dmitry Nelepov

I found a solution.

CollectionViewCell has a delegate preferredLayoutAttributesFittingAttributes

So, this function gives you layoutAttributes sets as estimatedItemSize

You need do next (for Height)

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

Same you can do to prefer size, width so make constant height (frame.size.height = layoutAttributes.size.height).

If you just copy layoutAttributes and change width, will no effect!

You must call super.

Actually you are right here is the problem with the constraints priority.

To resolve the priority constraint issue you should update the imageView Leading priority 999 and also label Trailing priority 999 either programmatically or directly from InterFace Builder.

Update priority programmatically by using the KVConstraintExtensionsMaster that I have implemented to update any constraint without IBOutlet reference of Autolayout constraint.

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

After this Now you need to calculate the only height of the 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);;
}

Dmitry Nelepov's solution in swift.

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
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!