首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionViewCell systemLayoutSizeFittingSize返回不正确的宽度

问UICollectionViewCell systemLayoutSizeFittingSize返回不正确的宽度
EN

Stack Overflow用户
提问于 2015-02-23 17:54:18
回答 3查看 8.4K关注 0票数 10

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

代码语言:javascript
复制
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在单元格上使用特定宽度,需要做些什么?

我也看到了这篇文章(Specifying one Dimension of Cells in UICollectionView using Auto Layout),我相信这可能会让我的问题失效。也许UICollectionViewFlowLayout不是为只有一个动态维度的单元格设计的,但这仍然不能解释为什么单元格提供了不寻常的宽度。

EN

回答 3

Stack Overflow用户

发布于 2016-11-08 17:32:42

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

代码语言:javascript
复制
- (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;
}
票数 10
EN

Stack Overflow用户

发布于 2019-03-25 11:22:03

在我的例子中,它没有正确地计算宽度,因为我在调用systemLayoutSizeFittingSize方法之前遗漏了这两行:

代码语言:javascript
复制
[sizingCell setNeedsLayout];
[sizingCell layoutIfNeeded];

花了几个小时来发现它,所以希望它能对其他有类似问题的人有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2018-04-06 23:12:40

我的存储库中提供了一个SSCCE (示例):

https://github.com/knutvalen/sandbox-uicollectionview

下面是主代码的一小段。

Swift 4

代码语言:javascript
复制
var array = ["foo", "bar", "baz"]

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)

    if let myCell = cell as? MyCollectionViewCell {
        myCell.titleLabel.text = array[indexPath.item]
        myCell.subtitleLabel.text = "my subtitle for " + array[indexPath.item] + " is a very long text with many letters and words"
        return myCell
    }

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let forcedWidth: CGFloat = 200
    let myCell = MyCollectionViewCell()
    myCell.titleLabel.text = array[indexPath.item]
    myCell.subtitleLabel.text = "my subtitle for " + array[indexPath.item] + " is a very long text with many letters and words"
    myCell.subtitleLabel.preferredMaxLayoutWidth = forcedWidth - (myCell.myMargin * 2)
    let optimalSize = myCell.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
    let forcedSize = CGSize(width: forcedWidth, height: optimalSize.height)
    return forcedSize
}

输出

​

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28670951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档