UICollectionView cell subviews do not resize

后端 未结 10 1764
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 00:25

In a CollectionView, some cells should have an additional subview or layer. The CollectionView can be told to resize it\'s cells, thus all content

10条回答
  •  广开言路
    2020-11-29 00:56

    I'm adding subView and constraint programmatically, the following code works for me:

    lazy var imageView: UIImageView = { [unowned self] in
        let imageView = UIImageView(frame: self.contentView.frame)
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
    
        return imageView
    }() 
    
    func updateCellWith(image: UIImage) {
    
        contentView.subviews.forEach { $0.removeFromSuperview() }
    
        contentView.addSubview(imageView)
        imageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
        imageView.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 0).isActive = true
        imageView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0).isActive = true
        imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
    
        self.imageView.autoresizingMask.insert(.flexibleHeight)
        self.imageView.autoresizingMask.insert(.flexibleWidth)
    
        imageView.image = image
    
    }
    

提交回复
热议问题