CollectionView单元格中的图像视图不尊重.scaleAspectFit

时间:2017-10-11 13:05:11

标签: ios swift uiimageview uicollectionviewcell

我正在以编程方式将图像视图添加到集合视图单元格。问题是某些图像不符合图像视图上的.scaleAspectFit规则,并且显示为边缘变焦。

这是我的手机课程:

    class CellVisitorGallery: UICollectionViewCell {

        let imgView: UIImageView = {
            let iv = UIImageView()
            iv.contentMode = .scaleAspectFit
            iv.image = UIImage(named: "fath_logo")
            iv.clipsToBounds = true
            return iv
        }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        addSubview(imgView)
        imgView.translatesAutoresizingMaskIntoConstraints = false
        imgView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
        imgView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        imgView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
        imgView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true

        // Or
        //imgView.frame = CGRect(x: 0, y: 0, width: contentView.frame.width, height: contentView.frame.height)
        //imgView.center = contentView.center
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

我尝试了锚点约束和帧中心,但两种方式都给出了相同的结果。

0 个答案:

没有答案