我在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
}
}
答案 0 :(得分:0)
替换此
let height = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
使用以下代码
let height =cell.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize, withHorizontalFittingPriority: UILayoutPriorityDefaultHigh, verticalFittingPriority: UILayoutPriorityDefaultLow)