I've got a collection view that's set up as a single horizontal row of cells. It's throwing the following constraint error (I'm using AutoLayout):
The behavior of the UICollectionViewFlowLayout is not defined because the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
I've Googled around and looked on SO, and everyone suggests that it's fixed by simply overriding the UIScrollViewDelegate methods:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
// iPhone 6+ device width handler
CGFloat multiplier = (screenWidth > kNumberOfCellsWidthThreshold) ? 4 : 3;
CGFloat size = screenWidth / multiplier;
return CGSizeMake(size, size);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
But it's still not working. It appears to be gawking at the fact that the cell height is taller than it's container, even though I'm setting the cell to the same hight as the container. Here's the rest of the error:
The relevant UICollectionViewFlowLayout instance is UICollectionViewFlowLayout: 0x7fa6943e7760, and it is attached to MyCollectionView: 0x7fa69581f200; baseClass = UICollectionView;
frame = (0 0; 375 98); clipsToBounds = YES; autoresize = RM+BM; layer = CALayer: 0x7fa694315a60; contentOffset: {0, 0}; contentSize: {0, 134}
Setting the cell manually to a drastically smaller size (e.g. size - 50.0
seems to work, but why can't I set the cell size to the same height as its container?
Turns out the missing piece from my question was the fact that this UICollectionView
is nested inside a UITableView
. iOS 8 introduced layoutMargins
in place of content offset values.
Per this question: iOS 8 UITableView separator inset 0 not working
I had to override my containing table view's cell margins:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
// Explictly set your cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With