【问题标题】:The behavior of the UICollectionViewFlowLayout is not definedUICollectionViewFlowLayout 的行为未定义
【发布时间】:2015-12-16 06:08:05
【问题描述】:

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

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

我用谷歌搜索并查看了 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);
}

但它仍然无法正常工作。尽管我将单元格设置为与容器相同的高度,但单元格高度高于其容器这一事实似乎令人目瞪口呆。这是错误的其余部分:

相关的 UICollectionViewFlowLayout 实例为 UICollectionViewFlowLayout: 0x7fa6943e7760,并附加到 MyCollectionView: 0x7fa69581f200;基类 = UICollectionView;

帧 = (0 0; 375 98); clipsToBounds = YES;自动调整大小 = RM+BM; 层=CALayer:0x7fa694315a60;内容偏移:{0, 0};内容大小:{0, 134}

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

【问题讨论】:

  • 您能否添加屏幕截图您遇到了什么错误以及您如何查看外观线以及您的要求。
  • @iOS_DK 我已经在问题中添加了错误;如果您愿意,我将添加一个集合视图的示例,但它不会告诉您超出我已经提供的任何内容。

标签: ios objective-c autolayout uicollectionview uicollectionviewlayout


【解决方案1】:

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

根据这个问题:iOS 8 UITableView separator inset 0 not working

我必须覆盖包含表格视图的单元格边距:

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

【讨论】:

  • OP提到了CollectionView,但答案是TableView。
  • 阅读答案。 CollectionView 嵌套在 inside TableView 中。
【解决方案2】:

快速覆盖单元格

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

【讨论】:

    猜你喜欢
    • 2016-01-30
    • 2016-03-19
    • 2020-03-13
    • 2014-09-10
    • 2015-11-11
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多