如何获得iOS 8的UICollectionViewCells的自动布局大小? (在iOS 8中,systemLayoutSizeFittingSize返回高度为零的大小)

由于iOS 8 [UIColletionViewCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]返回高度为0的大小。

代码如下:

要确定iOS 7中UICollectionView中单元格的大小,我使用systemLayoutSizeFittingSize:在使用自动布局的xib文件中定义的单元格上。 大小取决于作为我的xib文件中的UICollectionViewCell的子视图的UILabel的字体大小。 标签的字体被设置为UIFontTextStyleBody 。 所以基本上单元格的大小取决于在iOS 7中设置的字体大小。

这是代码本身:

 + (CGSize)cellSize { UINib *nib = [UINib nibWithNibName:NSStringFromClass([MyCollectionViewCell class]) bundle:nil]; // Assumption: The XIB file only contains a single root UIView. UIView *rootView = [[nib instantiateWithOwner:nil options:nil] lastObject]; if ([rootView isKindOfClass:[MyCollectionViewCell class]]) { MyCollectionViewCell *sampleCell = (MyCollectionViewCell*)rootView; sampleCell.label.text = @"foo"; // sample text without bar [sampleCell setNeedsLayout]; [sampleCell layoutIfNeeded]; return [sampleCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; } return CGSizeZero; } 

它在iOS 7中运行得非常好,但在iOS 8中运行得不错。不幸的是,我不知道为什么。

我怎样才能获得iOS 8的UICollectionViewCells的自动布局大小?

PS:使用

  return [sampleCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 

代替

  return [sampleCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 

就像有人可能会build议的那样,没有任何区别。

看起来像这个正式的错误:我提交了一个报告,作为这个副本closures

将Beta 6退出时报告。

[ 更新 :在iOS 8的GM种子中正常工作,并且该错误已被Appleclosures。]

你需要做的是将所有的内容包装在容器视图中,然后调用:

 return [sampleCell.containerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 

你的单元格应该看起来像这样:cell – > containerView – > sub views

这适用于ios7和ios8。

假设在ios8上,你所要做的就是设置estimateSize&cell将自动自动resize。 显然,这不是testing版6。

我们现在正在使用一个解决方法,在下面复制。 希望这些问题将在iOS 8发布之前解决,我们可以删除这个问题。 (kludge假设了苹果隐含的contentView行为的知识,并且我们必须破解IB出口引用,以便转移我们所传递的任何限制。)

我们注意到他们在升级过程中也从故事板/ NIBs中移除了所有的autoresizingMasks,这是有道理的,因为它应该是自动布局的,但是收集视图仍然会反弹到spring和struts。 也许这在净化中被忽略了?

– 担

 /** Kludge around cell sizing issues for iOS 8 and deployment to iOS 7 when compiled for 8. Call this on the collection view cell before it is used, such as in awakeFromNib. Because this manipulates top-level constraints, any references to such initial constraints, such as from IB outlets, will be invalidated. Issue 1: As of iOS 8 Beta 5, systemLayoutSizeFittingSize returns height 0 for a UICollectionViewCell. In IB, cells have an implicit contentView, below which views placed in IB as subviews of the cell are actually placed. However, constraints set between these subviews and its superview are placed on the cell, rather than the contentView (which is their actual superview). This should be OK, as a constraint among items may be placed on any common ancestor of those items, but this is not playing nice with systemLayoutSizeFittingSize. Transferring those constraints to be on the contentView seems to fix the issue. Issue 2: In iOS 7, prior to compiling against iOS 8, the resizing mask of the content view was being set by iOS to width+height. When running on iOS 7 compiled against iOS 8 Beta 5, the resizing mask is None, resulting in constraints effecting springs for the right/bottom margins. Though this starts out the contentView the same size as the cell, changing the cell size, as we do in the revealing list, is not tracked by changing it's content view. Restore the previous behavior. Moving to dynamic cell sizing in iOS 8 may circumvent this issue, but that remedy isn't available in iOS 7. */ + (void)kludgeAroundIOS8CollectionViewCellSizingIssues:(UICollectionViewCell *)cell { // transfer constraints involving descendants on cell to contentView UIView *contentView = cell.contentView; NSArray *cellConstraints = [cell constraints]; for (NSLayoutConstraint *cellConstraint in cellConstraints) { if (cellConstraint.firstItem == cell && cellConstraint.secondItem) { NSLayoutConstraint *parallelConstraint = [NSLayoutConstraint constraintWithItem:contentView attribute:cellConstraint.firstAttribute relatedBy:cellConstraint.relation toItem:cellConstraint.secondItem attribute:cellConstraint.secondAttribute multiplier:cellConstraint.multiplier constant:cellConstraint.constant]; parallelConstraint.priority = cellConstraint.priority; [cell removeConstraint:cellConstraint]; [contentView addConstraint:parallelConstraint]; } else if (cellConstraint.secondItem == cell && cellConstraint.firstItem) { NSLayoutConstraint *parallelConstraint = [NSLayoutConstraint constraintWithItem:cellConstraint.firstItem attribute:cellConstraint.firstAttribute relatedBy:cellConstraint.relation toItem:contentView attribute:cellConstraint.secondAttribute multiplier:cellConstraint.multiplier constant:cellConstraint.constant]; parallelConstraint.priority = cellConstraint.priority; [cell removeConstraint:cellConstraint]; [contentView addConstraint:parallelConstraint]; } } // restore auto-resizing mask to iOS 7 behavior contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [cell setNeedsUpdateConstraints]; [cell updateConstraintsIfNeeded]; } 

这是在iOS7设备上运行的xCode6和iOS 8 SDK中的一个bug :)它几乎是我的一天。 最后它在UICollectionViewCell子类中使用下面的代码。 我希望这将在下一个版本中得到解决

 - (void)setBounds:(CGRect)bounds { [super setBounds:bounds]; self.contentView.frame = bounds; } 

对于UITableViewCells和iOS 7(ios8完美地工作),我有同样的问题,但“Triet Luong”解决scheme为我工作:

 return [sampleCell.containerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 

这不是一个错误,我一直在努力解决这个问题一段时间,并尝试了不同的东西,我给下面的步骤,这对我的工作:

1)使用contentView [sampleCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

2)你可以创build你自己的contentView,并将子视图添加到contentView,如果你使用自定义的contentView到superView,不要忘记钉住contentView的顶部,底部和左侧和右侧做这个高度将是0,这里也取决于你的要求,你可以。 例如,不要将contentView的底部固定到superView,以便视图的高度可以变化,但是钉住是很重要的,并且根据您的要求来定位哪一个。

3)根据您的需求,在界面构build器中正确设置约束,在界面构build器中存在某些无法添加的约束,但是您可以将它们添加到viewController的viewDidLoad中。

所以我的2美分的input是约束必须在界面生成器systemLayoutSizeFittingSize适当地设置:UILayoutFittingCompressedSize返回正确的尺寸,这是解决scheme的家伙。

也许你有一些错误的约束, 你应该指定你的UIView和contentView之间的顶层空间和底层空间 ,我之前也有这个问题,那是因为我只是指定了顶层空间,并没有规定底层空间到contentView

这是iOS 8 beta版本中的一个错误。 最后它修复了iOS 8 GB(Build 12A365)。 所以对于我现在它与我为iOS 7编写的相同的代码工作。(请参阅问题)