NSLayoutConstraint更改后,systemLayoutSizeFittingSize不起作用

时间:2015-07-24 04:13:11

标签: ios swift uiimageview autolayout nslayoutconstraint

我在swift上使用Autolayout,我希望动态更改UIImageView UICollectionViewCell的高度。

我尝试过以下。 但是,似乎systemLayoutSizeFittingSize无效。

class CellImage: UICollectionViewCell {
    @IBOutlet weak var imageHeight:NSLayoutConstraint!
     .
     .
     .
    internal func setup() {
        self.imageHeight.constant = 200
        self.layoutIfNeeded()
    }
     .
     .
     .
    class func sizeForRow(#collectionView: UICollectionView, content: Content) -> CGSize {
        var cell = UINib(nibName:"name", bundle: nil).instantiateWithOwner(self, options: nil).first as! CellImage
        let width = CGRectGetWidth(collectionView.bounds)
        cell.frame.size.width = width
        cell.setup()
        let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
        return CGSizeMake(width, height) // not correct
    }
}

1 个答案:

答案 0 :(得分:0)

替换此

let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height

使用以下代码

let height =cell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize, withHorizontalFittingPriority: UILayoutPriorityDefaultHigh, verticalFittingPriority: UILayoutPriorityDefaultLow)