Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last Cell in UICollectionView is clipped

I have a UICollectionView that displays a vertical list of cells. It's basically a tableview but I'm using the collection view for more controller of how each cell is placed in the view. For some reason, the last cell in the collection view is clipped. I can drag up to see the bottom half, but as soon as I let go the collection view bounces back into place, obscuring the bottom half of the last cell. I have a xib that contains the collectionview, and then I register collection view cells from another xib. This whole view is placed in a navigation controller programmatically. My thoughts are that it has to be an issue with autolayout. Since I'm adding the view controller containing the collection view to a navigation controller programmatically, no constraints are set between the collection view and the navigation bar. If I hide the instantiated navigation bar and just drag one out in IB, the collection view cell is not clipped. But I don't want to do it this way. I've tried programmatically adding constraints, but that doesn't work either. Does anyone have any suggestions? Here's how I'm creating the navigation controller and placing the view controller containing the collection view into it. Also, the code I've tried to use to add constraints is include. For the time being, I'm doing this in the app delegate in didFinishLaunchingWithOptions:

self.homeViewController = [[FFHomeViewController alloc] init];
FFNavigationPanelViewController *navigationController = [[FFNavigationPanelViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController];
//    frontNavigationController.navigationBarHidden = YES;
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:navigationController];


NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:self.homeViewController.collectionView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.homeViewController.navigationController.navigationBar attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];

[self.viewController.frontViewController.view addConstraint:topConstraint];
like image 764
mike Avatar asked Aug 23 '13 18:08

mike


2 Answers

Ran into similar issue, I have horizontal collectionView with one row that I use in many places, somehow In one ViewController collectionView was clipping half of last cell also that cell wasn't clickable.

I was able to easily fix this by setting sectionInset of collectionView.

Just put this code in viewDidLoad() if your collectionView is in ViewController or init method if it's in xib.

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
  flowLayout.sectionInset.right = flowLayout.itemSize.width / 2
}

In my case missing empty space in collectionView.contentSize was nearly equal to half of cell width, but you can set it to whatever you want.

I used .right property because I scroll cells horizontally, in case of vertically scrolled collectionView just use .bottom property.

Not very pretty fix, but at least it works.

like image 123
szooky Avatar answered Nov 13 '22 18:11

szooky


I just ran into a similar issue. The problem stems from the fact that the UICollectionView is a subclass of UIScrollView and for some reason the amount of space assigned for content is not getting updated to correspond the actual number of cells you have.

In your case, with a single column of items, you need to set the contentSize of your UICollectionView to match the amount of space you need to virtually display all of the UICollectionViewCells.

Assuming your cells are 100 px high and you have only one section, I would add something like the following to the ViewWillAppear in the View Controller:

int itemCount = [self.collectionView numberOfItemsInSection:0];
float contentHeight = (100*itemCount)+100;
CGRect screenFrame = [[UIScreen mainScreen]bounds];
self.collectionView.contentSize = CGSizeMake(screenFrame.size.width, frameHeight);

Adding the extra 100 allows for some empty space at the end when you scroll to the bottom.

Side note: I may have run into an odd bug here. When I put these calculations into any of of the viewWillLoad|viewWillAppear|viewDidAppear it didn't seem to take effect, but strangely, putting it into numberOfItemsInSection does work. That's really not the right place for it but may be side effect of loading the collectionView from a xib rather than creating it programmatically.

And a final followup: Putting the contentSize code into viewWillLayoutSubviews seems to get the appropriate behaviour.

like image 1
Erik Ableson Avatar answered Nov 13 '22 17:11

Erik Ableson