Так как iOS 8 [UIColletionViewCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]
возвращает размер с высотой 0.
Вот что делает код:
Чтобы определить размер ячейки в UICollectionView
в iOS 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;
}
Он отлично работает в iOS 7, но не в iOS 8. К сожалению, я не знаю, почему.
Как получить размер макета для UICollectionViewCells в iOS 8?
PS: Использование
return [sampleCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
вместо
return [sampleCell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
как может предложить кто-то, не имеет никакого значения.