0

I am currently testing in xcode a UICollectionView with just one horizontal row like a kinda cover flow. Basically I have my own Custom Cell class and xib file for the cell and then on each cell I am adding another UIView with a xib. In case you are wondering why, it is so I can add different UIViews to the cell. Right now I am only adding one.

Edit I have followed the WWDC 2012 video on creating a linelayout of a UICollectionViewCell with one difference. Instead of the cell in the middle getting bigger all the other cells get smaller.

Everything below is new to my question.

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {

NSArray *array = [super layoutAttributesForElementsInRect:rect];
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;

    for (UICollectionViewLayoutAttributes *attributes in array){

    if (CGRectIntersectsRect(attributes.frame, rect)) {
        CGFloat distance = CGRectGetMidX(visibleRect) - attributes.center.x;
        CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
        if (ABS(distance) < ACTIVE_DISTANCE) {
            //THIS WOULD MAKE THE MIDDLE BIGGER
            //CGFloat zoom = 1 + ZOOM_FACTOR *(1- ABS(normalizedDistance));
            //attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 1.0);
            //attributes.zIndex = round(zoom);
        } else {
            //THIS MAKES ALL THE OTHERS NOT IN THE RECT SMALLER
            CGFloat zoom = 1 + ZOOM_FACTOR *(1- ABS(normalizedDistance));
            attributes.transform3D = CATransform3DMakeScale(zoom, zoom, 1.0);
            attributes.zIndex = round(zoom);
        }
    }
}

return array;

}

The problem can be seen in the attached image.

Reused cell not resizing

Pink = Collection View Size

Brown = Cell size

Green = Cells Content size and an attached xib to the content size.

The problem I THINK I have is with the layout. When the sell is dequeued it is made smaller by the above code. Then when it is reused the CELL gets bigger again but the content view does not.

I have tired to manually set the frame of the content view but that does nothing.

UPDATE 1: This also only happens when I add a xib to the Cells content view. If there is no subview to the content view then there is no problem

UPDate 2: It appears that the subview of the cell, my xib is not resizing. I have tried to manually change its frame size but the only place this helps is in the cells drawrect method which feels like a hack to me.

darbid
  • 2,184
  • 19
  • 44

2 Answers2

1

reused cell not able to redraw itself so give call to

-(void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    [self setNeedsDisplay]; // force drawRect:
}

from cellForItemAtIndexPath of the UICollectionView. also have a look at this link

and this question

Community
  • 1
  • 1
  • Thank you Harsh. Whilst I think your answer is good and it might have helped in this case and for no reason I understand fixing my auto layout constraints did in fact fix it. – darbid Dec 27 '12 at 16:26
  • Actually Harsh neither your solution nor mine helped. I hope the right thing to do is to use this thread to find a solution instead of open a new question. I will edit above. – darbid Jan 04 '13 at 18:00
0

My answer is very specific and I am not sure it will help anyone.

The problem was that I had a constraint on the bottom of the grey view. After I changed this constraint to a less than or equal too then for some reason it worked.

Now I know this does not explain why it was not happening to every cell but it fixed my problem.

As such Harsh's answer might also be worth looking at if you have landed here after doing a search.

Edit: there also appears to be some bugs in the 6.0 UiCollectionView controller which seem to be fixed in 6.1

darbid
  • 2,184
  • 19
  • 44