README and documentationIGListKit version: 2.2.0how 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 ?
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?
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