UICollectionViewFlowLayout 的行为未定义

The behavior of the UICollectionViewFlowLayout is not defined

我有一个集合视图,它设置为单行水平单元格。它抛出以下约束错误(我正在使用 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.

我用 Google 搜索并查看了 SO,每个人都建议通过简单地覆盖 UIScrollViewDelegate 方法来修复它:

- (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);
}

但是还是不行。尽管我将单元格设置为与容器相同的高度,但单元格高度比容器高这一事实似乎令人目瞪口呆。这是错误的其余部分:

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}

手动将单元格设置为非常小的尺寸(例如 size - 50.0 似乎可行,但为什么我不能将单元格尺寸设置为与其容器相同的高度?

原来我的问题中缺少的部分是 UICollectionView 嵌套在 UITableView 中。 iOS 8 引入 layoutMargins 代替内容偏移值。

针对这个问题:iOS 8 UITableView separator inset 0 not working

我不得不覆盖包含 table 视图的单元格边距:

-(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];
    }
}

swift 覆盖单元格

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