未定义UICollectionViewFlowLayout的行为

bra*_*ipt 5 objective-c ios autolayout uicollectionview uicollectionviewlayout

我有一个集合视图,它被设置为单个水平行单元格.它抛出以下约束错误(我正在使用AutoLayout):

未定义UICollectionViewFlowLayout的行为,因为项高度必须小于UICollectionView的高度减去截面插入顶部和底部值,减去内容插入顶部和底部值.

我用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);
}
Run Code Online (Sandbox Code Playgroud)

但它仍然无法正常工作.尽管我将细胞设置为与容器相同的高度,但细胞高度比它的容器高,这似乎是一种愚蠢的行为.这是错误的其余部分:

相关的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似乎工作,但为什么我不能将单元格大小设置为与其容器相同的高度?

bra*_*ipt 5

事实证明,我的问题中缺少的部分UICollectionView是嵌套在一个内部的事实UITableView.引入iOS 8 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'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];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 阅读答案.CollectionView是嵌套的_inside_ TableView. (3认同)