Nick Nick - 4 months ago 26
Objective-C Question

Peek & Pop - Pop leading to wrong cell in UICollectionView

I have a UITableViewController that is embedded in a UINavigationController and I'm trying to implement Peek & Pop into the TableView. I have the "peek" part working perfectly, but when I try to "pop" into the next ViewController, the cell I was "peeking" and the next cell over are both shown. I am "popping" into a UICollectionView, and as I mentioned, the "peek" half shows the correct cell, but the "pop" does not. This issue only happens when I use

[self.navigationController showViewController:viewControllerToCommit sender:nil];
or
[self.navigationController pushViewController:viewControllerToCommit animated:YES];
to execute the "pop".

Here is the "Peek" showing the correct cell
Peek showing correct cell

And the "Pop" showing the wrong cell(s)

Pop showing wrong cell

I tried to use
[self presentViewController:viewControllerToCommit animated:YES completion:nil];
and the correct cell is shown, except this doesn't give me the navigational elements that I need, so I can't use it (unless there is a way to get all the navigational elements back).

My initial thought is that there is something wrong with how my app is figuring out the size of the CollectionViewCell. Here is the code that I'm using for that, though it appears that it works properly with everything other than Peek & Pop.

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize collectionViewBounds = collectionView.bounds.size;

int navigationHeight = self.navigationController.navigationBar.bounds.size.height;
int toolbarHeight = self.navigationController.toolbar.bounds.size.height;
int statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;

int cellHeight = collectionViewBounds.height - (navigationHeight + statusBarHeight + toolbarHeight);
int cellWidth = collectionViewBounds.width;

return CGSizeMake(cellWidth, cellHeight);
}


To add to my confusion, the "pop" works perfectly when the first or last item in the TableView are "peeked". Any help with this would be greatly appreciated.

Answer

So I finally figured out what was causing this problem. My app is a Universal app, and I use a Popover Segue on iPads. In viewWillAppearof my ViewController that is "popping" incorrectly, I use [self setPreferredContentSize:CGSizeMake(400.0, 600.0)] to determine the size of the Popover on an iPad. Once I removed that line, my Peek & Pop worked perfectly.

I ended up adding a new property to my ViewController @property BOOL fromPeek and set that property to YES in - (UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location of my previewing ViewController. Finally, I modified my viewWillAppear to be if(!fromPeek) [self setPreferredContentSize:CGSizeMake(400.0, 600.0)]; and the problem is now solved!