How to find a UICollectionViewCell's coordinates relative to the App Window

11,686

Solution 1

This answer is very late, but hopefully it will help someone. Dan, you have the right idea, but it is even easier that what you have. Just replace

CGRect frame = CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height);
frame.origin = [cell convertPoint:cell.frame.origin toView:self.view];

with

CGRect frame = [collectionView convertRect:cell.frame toView:self.view];

The problem was that you were calling the convert method on the cell, not the collectionView.

Solution 2

If I understand you correctly, I think the problem is that you first have to correct for the scroll view's contentOffset, which is easy to do since UICollectionView is a subclass of UIScrollView.

CGRect frame = CGRectMake(0.0,
                          0.0,
                          cell.frame.size.width,
                          cell.frame.size.height);
CGPoint correctedOffset = 
 CGPointMake(cell.frame.origin.x - collectionView.contentOffset.x,
             cell.frame.origin.y - collectionView.contentOffset.y);
frame.origin = [cell convertPoint:correctedOffset toView:self.view];

I tested this in one of my UICollectionView demo apps and it seems to work. Give it a try.

Solution 3

Just in case if anybody looking for this, here is how it can be done in Swift:

let cellFrameInSuperview = collectionView.convertRect(collectionView.cellForItemAtIndexPath(indexPath)!.frame, toView: view)
Share:
11,686
Dan Lister
Author by

Dan Lister

Updated on June 06, 2022

Comments

  • Dan Lister
    Dan Lister almost 2 years

    Is it possible to translate a UICollectionViewCell's coordinates from being relative to the UICollectionView to being relative to the superview? So far I've been unable to translate a UICollectionViewCell's coordinates when touched to the superview. Here's what I've tried so far:

    - (void)collectionView:(UICollectionView *)collectionView  
            didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
        UICollectionViewCell *cell = 
         [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" 
                                                   forIndexPath:indexPath];
    
        CGRect frame = CGRectMake(0.0, 
                                  0.0,  
                                  cell.frame.size.width, 
                                  cell.frame.size.height);
        frame.origin = [cell convertPoint:cell.frame.origin 
                                   toView:self.view];
    }
    

    I'd like to create a new frame which originates from the cell's position on the view, not in the UICollectionView. Any help would be great thanks.

  • Kyle Clegg
    Kyle Clegg over 10 years
    Perfection. I seriously tried 50 other things before this. Thanks. #latenightprogramming
  • Thijs van der Heijden
    Thijs van der Heijden about 4 years
    Wow thanks! I kept trying to just call convertTo() without calling it on a certain object, not figuring out why it wouldn't work. Stupid me haha!