Reputation: 1816
I'm trying to show a detail view popover pointing to UICollectionViewCell
. The popover is always shown from upper left corner. How do I get it to point the arrow to the selected cell? Here is the code:
-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
JCCollectionData *favorite = [self.dataSource objectAtIndex:indexPath.row];
JCCollectionCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
JCContactDetailViewController *contactDetail = [[JCContactDetailViewController alloc] init];
contactDetail.Contact = favorite;
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:contactDetail];
nav.modalPresentationStyle = UIModalPresentationPopover;
nav.popoverPresentationController.delegate = self;
nav.popoverPresentationController.sourceView = cell;
nav.popoverPresentationController.sourceRect = [collectionView layoutAttributesForItemAtIndexPath:indexPath].frame;
nav.preferredContentSize = CGSizeMake(500, 400);
nav.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
[self presentViewController:nav animated:YES completion:nil];
}
Upvotes: 0
Views: 295
Reputation: 4711
This is because in these two lines:
nav.popoverPresentationController.sourceView = cell;
nav.popoverPresentationController.sourceRect = [collectionView layoutAttributesForItemAtIndexPath:indexPath].frame;
you are saying the source view is the cell and then giving the frame of the cell as the source rect which will be in relation to it's superview not itself.
Instead you could cell.bounds
or just build a CGRect which has it's origin at 0,0 and size of the cells frame.
EDIT
I've just noticed something else. You have this line:
JCCollectionCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
That's dequeuing a new instance of a cell not getting the one that was the source of the selection.
Instead you should do something like this:
JCCollectionCellView *cell = (JCCollectionCellView*)[collectionView cellForItemAtIndexPath:indexPath];
That should get you the actual cell that generated the selection.
(I think the syntax is correct but my Objective C is getting a bit rusty now that I am using Swift full time. It should be enough to get you pointed in the correct direction though.)
Upvotes: 2