如何在旋转时如何将UICollectionViewCell的背景图像转换为AspectFit单元格?

时间:2018-07-31 16:23:30

标签: swift uicollectionview uicollectionviewcell ios-autolayout

我有一个带有自定义单元格的collectionview。
enter image description here

如何在旋转时使图像充满collectionviewcell? 收集单元中的初始化代码:

override init(frame: CGRect) {
    super.init(frame: frame)
    let imageView = UIImageView(frame: contentView.frame)
    imageView.image = #imageLiteral(resourceName: "images")
    imageView.contentMode = .scaleAspectFill
    imageView.adjustsImageSizeForAccessibilityContentSizeCategory = false
    imageView.clipsToBounds = true
    contentView.addSubview(imageView)


    contentView.addSubview(titleLabel)
    let guide = contentView.layoutMarginsGuide

    imageView.centerXAnchor.constraint(equalTo: guide.centerXAnchor).isActive = true
    imageView.rightAnchor.constraint(equalTo: guide.rightAnchor).isActive = true
    imageView.leftAnchor.constraint(equalTo: guide.leftAnchor).isActive = true
    imageView.widthAnchor.constraint(equalTo: guide.widthAnchor).isActive = true

    //title.topAnchor.constraint(equalTo: guide.topAnchor, constant: 10).isActive = true
    //title.leadingAnchor.constraint(equalTo: guide.leadingAnchor, constant: 10).isActive = true
}

enter image description here

1 个答案:

答案 0 :(得分:0)

  collectionView.invalidateLayout()