Iglistkit: How to make dynamic height and fix width equal to collection view frame width

Created on 19 Feb 2017  路  3Comments  路  Source: Instagram/IGListKit

New issue checklist

General information

  • IGListKit version: 2.2.0
  • iOS version(s): 10+
  • CocoaPods/Carthage version:
  • Xcode version: 8+
  • Devices/Simulators affected: Simulators
  • Reproducible in the demo project? (Yes/No): NO
  • Related issues:

how to make dynamic height and fix width in cell ?
in controller my code is:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.estimatedItemSize = CGSizeMake(self.collectionView.frame.size.width, 137); self.collectionView.collectionViewLayout = layout;

is section controller my code is:

- (CGSize)sizeForItemAtIndex:(NSInteger)index {

    CGFloat width = self.collectionContext.containerSize.width;
    CGFloat height = 137;

    CGSize result = CGSizeMake(width, height);
    return result;
}

and in collection cell:

-(UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{

    if (_isCalculate == false) {

       // [super preferredLayoutAttributesFittingAttributes:layoutAttributes];

        [self setNeedsLayout];
        [self layoutIfNeeded];

        CGSize size = [self.contentView systemLayoutSizeFittingSize:layoutAttributes.size];

        CGRect newFrame = layoutAttributes.frame;
        newFrame.size.width = (CGFloat)ceilf(size.width);

        NSLog(@"%@", newFrame);

        layoutAttributes.frame = newFrame;
        _isCalculate = true;


    }  
    return layoutAttributes;
}

And my cell is not fill width equal to collection view width, can you help me ?

question

Most helpful comment

I think you shouldn't change the width of the newFrame, you should just change the height only. As in the sample code
https://github.com/Instagram/IGListKit/blob/master/Examples/Examples-iOS/IGListKitExamples/Views/FullWidthSelfSizingCell.swift#L44

    override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        setNeedsLayout()
        layoutIfNeeded()
        let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
        var newFrame = layoutAttributes.frame
        // note: don't change the width
        newFrame.size.height = ceil(size.height)
        layoutAttributes.frame = newFrame
        return layoutAttributes
    }

All 3 comments

I think you shouldn't change the width of the newFrame, you should just change the height only. As in the sample code
https://github.com/Instagram/IGListKit/blob/master/Examples/Examples-iOS/IGListKitExamples/Views/FullWidthSelfSizingCell.swift#L44

    override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        setNeedsLayout()
        layoutIfNeeded()
        let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
        var newFrame = layoutAttributes.frame
        // note: don't change the width
        newFrame.size.height = ceil(size.height)
        layoutAttributes.frame = newFrame
        return layoutAttributes
    }

edwinkcw

Thanks, you saved my day, it's work perfectly ))

@edwinkcw any idea why setNeedsLayout and layoutIfNeeded are obligatory for valid size?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shuhrat10 picture shuhrat10  路  3Comments

krysztalzg picture krysztalzg  路  3Comments

FazeelAkhtar picture FazeelAkhtar  路  3Comments

iwasrobbed picture iwasrobbed  路  3Comments

rnystrom picture rnystrom  路  3Comments