Reputation: 808
I would like to get a UICollectionViewCell
's position after programattically scrolling to a cell. I'm setting the scroll position like this.
[self.collectionView scrollToItemAtIndexPath:indexPath
atScrollPosition:UICollectionViewScrollPositionCenteredVertically
animated:NO];
I would then like to get the frame of the cell I have scrolled to as I need to position another UIView
near it.
This code returns the rect of the cell with the scrollview, but I need the position within the visible rect.
UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGRect cellRect = attributes.frame;
Upvotes: 0
Views: 5628
Reputation: 489
Here is the cell X and Center values
UICollectionViewLayoutAttributes *attributes = [collectionView layoutAttributesForItemAtIndexPath:indexPath];
CGPoint cellCenterPoint = attributes.center;
CGPoint contentOffset = [collectionView contentOffset];
NSLog(@"%f , %f",-1*contentOffset.x , cellCenterPoint.x);
int relativeCellCenter = (-1*contentOffset.x) + cellCenterPoint.x +collectionView.frame.origin.x;
int relativeX = (-1*contentOffset.x) +collectionView.frame.origin.x;
Upvotes: 0
Reputation: 119242
The frame of a cell doesn't change, it's the content offset of the enclosing scroll view. Subtract the contentOffset
from the frame's origin to get the on-screen position, or use one of the convertRect:
methods to get the coordinates in a related view.
Upvotes: 5