UICollectionViewFlowLayout behavior not defined

I have a collection view that is configured as one horizontal row of cells. It throws the following constraint error (I use AutoLayout):

The behavior of the UICollectionViewFlowLayout is undefined because the height of the element must be less than the height of the UICollectionView minus the upper and lower values ​​of the sections, minus the upper and lower values ​​of the content insert.

I have Googled and looked at SO, and everyone says that this is fixed simply by 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 still does not work. It looks like he repents that the height of the cell is higher than the container, although I set the cell to the same height as the container. Here is the rest of the error:

UICollectionViewFlowLayout UICollectionViewFlowLayout: 0x7fa6943e7760, MyCollectionView: 0x7fa69581f200; baseclass= UICollectionView;

frame = (0 0; 375 98); clipsToBounds = YES; autoresize = RM + BM; layer = CALayer: 0x7fa694315a60; contentOffset: {0, 0}; contentSize: {0, 134}

(, size - 50.0, , , , ?

+4
2

, , UICollectionView UITableView. iOS 8 layoutMargins layoutMargins.

: iOS 8 UITableView 0

, :

-(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 margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    // Explictly set your cell layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
+5

swift

override func awakeFromNib() {
    super.awakeFromNib()
    self.separatorInset = .zero
    self.preservesSuperviewLayoutMargins = false
    self.layoutMargins = .zero
}
0

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


All Articles