Peek & Pop - Pop leading to wrong cell in UICollectionView

I have a UITableViewController built into the UINavigationController, and I'm trying to implement Peek and Pop in a TableView. I have a โ€œpeekโ€ part that works fine, but when I try to โ€œpopโ€ in the next ViewController, the cells that I was โ€œpeekingโ€ and the next cell are both shown. I โ€œpop upโ€ in the UICollectionView, and, as I mentioned, the โ€œpeekโ€ half shows the correct cell, but the โ€œpopโ€ does not. This problem occurs when I use [self.navigationController showViewController:viewControllerToCommit sender:nil]; or [self.navigationController pushViewController:viewControllerToCommit animated:YES]; to execute "pop".

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

And "Pop" showing the wrong cell
Show wrong cell display

I tried using [self presentViewController:viewControllerToCommit animated:YES completion:nil]; , and the correct cell is shown, except that it does not give me the navigation elements that I need, so I cannot use them (unless there is a way to get all the navigation elements back).

My initial thought was that something was wrong with how my application figures out the size of CollectionViewCell. Here is the code I use for this, although it seems to work correctly with everything except Peek and 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, pop works fine when the first or last item in a TableView peeks. Any help with this would be greatly appreciated.

+5
source share
1 answer

So, I finally understood what causes this problem. My application is a universal application, and I use Popover Segue on iPads. In the viewWillAppear my ViewController, which does not display correctly, I use [self setPreferredContentSize:CGSizeMake(400.0, 600.0)] to determine the size of the Popover on the iPad. Once I deleted this line, my Peek and Pop worked fine.

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

+2
source

Source: https://habr.com/ru/post/1243474/


All Articles