0
votes

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

1
have you tested using layoutifneeded after settingLayoutNeeded ? - santhosh
Yes i tried [self.view setNeedsLayout] and [self.view layoutIfNeeded] after invalidating the layout but nothing works. The strange that i see the cell grows in width if the textfield inside grows. but the problem happens when the cell should go to the next line. it doesen't - zizoodiesel
I noticed that if i scroll down the contentSize's height of the collectionView grows and that cell jumps to the next line but if i scroll Up nothing happens and until the cell disappers and the next time the cell reappear in the correct position - zizoodiesel

1 Answers

0
votes

So after two days of research I cant find a proper solution to this issue i managed to bypass it by setting the cell frame in layoutAttributesForElementsInRect like this:

 UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:[attributesForElementsInRect indexOfObject:attributes] inSection:0]];

if (!CGRectEqualToRect(cell.frame, refAttributes.frame))
    cell.frame = refAttributes.frame;

this is the complete function

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


    UICollectionViewCell* cell = [self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:[attributesForElementsInRect indexOfObject:attributes] inSection:0]];

    if (!CGRectEqualToRect(cell.frame, refAttributes.frame))
        cell.frame = refAttributes.frame;


}
}

Hope this will help somebody or someone have a better solution