UICollectionViewCell systemLayoutSizeFittingSize返回不正确的宽度 [英] UICollectionViewCell systemLayoutSizeFittingSize returns incorrect width

查看:505
本文介绍了UICollectionViewCell systemLayoutSizeFittingSize返回不正确的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩动态UICollectionViewCell并注意到在iOS 8上调用 cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) UICollectionViewCell 返回不正确的宽度。目前解决此问题的唯一解决方法是显式添加宽度约束以强制单元格的宽度。以下代码用于Cell子类:

I've been toying with dynamic UICollectionViewCell's and have noticed that on iOS 8 calling cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) on a UICollectionViewCell returns an incorrect width. Currently the only workaround that addresses this is to explicitly add a width constraint to force the cell's width. The below code is used in a Cell subclass:

func calculatePreferredHeightForFrame(frame: CGRect) -> CGFloat {
    var newFrame = frame
    self.frame = newFrame
    let widthConstraint = NSLayoutConstraint(item: contentView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: CGRectGetWidth(frame))
    contentView.addConstraint(widthConstraint)
    self.setNeedsUpdateConstraints()
    self.updateConstraintsIfNeeded()
    self.setNeedsLayout()
    self.layoutIfNeeded()
    let desiredHeight: CGFloat = self.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
    newFrame.size.height = CGFloat(ceilf(Float(desiredHeight)))
    contentView.removeConstraint(widthConstraint)
    return CGRectGetHeight(newFrame)
}

我知道,对于iOS 8和动态UICollectionViewFlowLayout,UICollectionViewCell的contentView以不同的方式处理约束,但是我在这里缺少什么?需要做些什么才能确保systemLayoutSizeFittingSize在单元格上使用特定宽度?

I know that with iOS 8 and dynamic UICollectionViewFlowLayout that the UICollectionViewCell's contentView handles constraints differently but is there something I'm missing here? What does one need to do to ensure that systemLayoutSizeFittingSize uses a particular width on a cell?

我也遇到过这篇文章(使用自动布局在UICollectionView中指定单元格的一个维度)并且相信这可能实际上使我无效题。也许UICollectionViewFlowLayout不是为只有一个动态维度的单元格设计的,但仍然无法解释为什么单元格给出了不寻常的宽度。

I also came across this post (Specifying one Dimension of Cells in UICollectionView using Auto Layout) and believe this might actually invalidate my question. Perhaps UICollectionViewFlowLayout is not designed for cells with only one dynamic dimension, but that still doesn't explain why the cell gives an unusual width.

推荐答案

我遇到了同样的问题。关键是在获取systemLayoutSize时使用优先级。

I've faced same issue. The key is using priorities when getting systemLayoutSize.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.prototypeCell) {
        self.prototypeCell = [TrainingMenuCell new];
        self.prototypeCell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    }

    [self configureCell:self.prototypeCell forIndexPath:indexPath];
    [self.prototypeCell setNeedsLayout];
    [self.prototypeCell layoutIfNeeded];

    CGFloat width = CGRectGetWidth(collectionView.frame) - 12;
    CGSize fittingSize = UILayoutFittingCompressedSize;
    fittingSize.width = width;

    CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:fittingSize withHorizontalFittingPriority:UILayoutPriorityRequired verticalFittingPriority:UILayoutPriorityDefaultLow];
    return size;
}

这篇关于UICollectionViewCell systemLayoutSizeFittingSize返回不正确的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆