Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 942
Nick Avatar asked Feb 20 '16 18:02

Nick


People also ask

Is it peek or peak?

It's easy to remember that the PEAK is the top or pinnacle of something. Just think of the A like the top of a mountain. A PEEK is a cheeky glance (or peep) – so you can think of the two EEs in CHEEKY, and the two EEs in PEEK – or like two eyes peeping.

What does having a peek mean?

If you peek at something or someone, you have a quick look at them, often secretly.

What does peek mean slang?

quick look, gander (informal), brief view, shufti (British, slang) in the sense of look.

Does peek mean look?

A peek is a glance or a quick look. It has a fun, furtive innuendo. It can also mean to glance or to peer at. It's frequently paired with sneak — I took a sneak peek at next quarter's sales projections.


1 Answers

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!

like image 156
Nick Avatar answered Oct 17 '22 04:10

Nick