I have a collectionView with a custom UICollectionViewFlowLayout. The last collectionview cell conatains an UITextField. the Cell is resizible according to the width of the textfield
- (IBAction)textFieldDidChange:(UITextField*)textField {
[_collectionView.collectionViewLayout invalidateLayout];
}
This is my code of the custom layout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* attributesForElementsInRect = [super layoutAttributesForElementsInRect:rect];
NSMutableArray* newAttributesForElementsInRect = [[NSMutableArray alloc] init];
// use a value to keep track of left margin
CGFloat leftMargin = 0.0;
for (id attributes in attributesForElementsInRect) {
UICollectionViewLayoutAttributes* refAttributes = attributes;
// assign value if next row
if (refAttributes.frame.origin.x == self.sectionInset.left) {
leftMargin = self.sectionInset.left;
} else {
// set x position of attributes to current margin
CGRect newLeftAlignedFrame = refAttributes.frame;
newLeftAlignedFrame.origin.x = leftMargin;
refAttributes.frame = newLeftAlignedFrame;
}
// calculate new value for current margin
leftMargin += refAttributes.frame.size.width + 8;
[newAttributesForElementsInRect addObject:refAttributes];
}
NSLog(@"newAttributesForElementsInRect : %@", newAttributesForElementsInRect);
return newAttributesForElementsInRect;
}
I want the cell that contains the textfield to go to the next line when it got big enough. the problem that layoutAttributesForElementsInRect:(CGRect)rect logs the correct desired frame but the cell won't update until i swipe it offscreen then swipe it back. I there something i can do to force the cell get its new frame immediately.
Thanks