全幅のシンプルな UICollectionViewCell があり、SnapKit を使用してサブビューをレイアウトしています。他のビューに SnapKit を使用していますが、うまく機能していますが、collectionViewCell 内では機能していません。これは私が達成しようとしているレイアウトです: collectionViewCell レイアウト
collectionViewCell のコードは次のとおりです。
lazy var imageView: UIImageView = {
let img = UIImageView(frame: .zero)
img.contentMode = UIViewContentMode.scaleAspectFit
img.backgroundColor = UIColor.lightGray
return img
}()
override init(frame: CGRect) {
super.init(frame: frame)
let imageWidth = CGFloat(frame.width * 0.80)
let imageHeight = CGFloat(imageWidth/1.77)
backgroundColor = UIColor.darkGray
imageView.frame.size.width = imageWidth
imageView.frame.size.height = imageHeight
contentView.addSubview(imageView)
imageView.snp.makeConstraints { (make) in
make.top.equalTo(20)
//make.right.equalTo(-20)
//make.right.equalTo(contentView).offset(-20)
//make.right.equalTo(contentView.snp.right).offset(-20)
//make.right.equalTo(contentView.snp.rightMargin).offset(-20)
//make.right.equalToSuperview().offset(-20)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
制約を適用しないと、imageView はセルの左上に表示されますが、制約を適用すると画像が消えます。デバッグ ビューで collectionViewCell を調べると、imageView と制約が表示されますが、「UIImageView のサイズと水平位置があいまいです」。
また、 contentView.translatesAutoresizingMaskIntoConstraints = false を設定しようとしましたが、結果は同じです。
私はすべてのコードを使用しており、ストーリーボードの UI やレイアウトは使用していません。
助けてくれてありがとう!