mar*_*_st 24 objective-c ios autolayout ios8
由于iOS 8 [UIColletionViewCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]返回高度为0的大小.
这是代码的作用:
为了确定UICollectionViewiOS 7中单元格的大小,我使用systemLayoutSizeFittingSize:自动布局在xib文件中定义的单元格上使用.大小取决于UILabel作为UICollectionViewCell我的xib文件中的子视图的字体大小.标签的字体设置为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;
}
Run Code Online (Sandbox Code Playgroud)
它在iOS 7中完美运行,但在iOS 8中运行不佳.不幸的是,我不知道为什么.
PS:使用
return [sampleCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
Run Code Online (Sandbox Code Playgroud)
代替
return [sampleCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
Run Code Online (Sandbox Code Playgroud)
有人可能会建议,没有任何区别.
小智 8
您需要做的是将所有内容包装在容器视图中,然后调用:
return [sampleCell.containerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
Run Code Online (Sandbox Code Playgroud)
您的单元格应如下所示:cell - > containerView - > sub views
这适用于ios7和ios8.
据说在ios8上,您所要做的就是设置estimateSize&cell将自动自动调整大小.显然,这不是beta 6的工作.