2017-05-31 8 views
0

私は単純なUICollectionViewCellを全幅に持っていて、私はSnapKitを使ってそのサブビューをレイアウトしています。私は他のビューのためにSnapKitを使用していますが、それは素晴らしいですが、collectionViewCell内では動作しません。これは私が達成しようとしているレイアウトです: collectionViewCell layoutSnapKitを使用したUICollectionViewCellレイアウト

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やレイアウトは使用していません。

ありがとうございました!

答えて

0

SnapKitについてよく分かりませんが、制約がありません。高さ、幅、x、yが必要です。 IOS 9の制約でうまく動作します。

let imageView: UIImageView = { 
     let view = UIImageView() 
     view.translatesAutoresizingMaskIntoConstraints = false 
     view.contentMode = .scaleAspectFit 
     view.backgroundColor = .lightGray 
     return view 
    }() 

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

    func setupViews() { 
     self.addSubview(imageView) 

     imageView.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true 
     imageView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -10).isActive = true 
     imageView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 2/3, constant: -10).isActive = true 
     imageView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1/2, constant: -10).isActive = true 
    } 
0

自動レイアウト(SnapKit)と手動レイアウト(セットフレーム)の両方を使用することはできません。通常、自動レイアウトを使用すると手動レイアウトが失敗します。ただし、snpの終了時の制約のコードは完全ではありません。自動レイアウトを使用するか手動レイアウトを使用するかにかかわらず、最終的にビューは位置とサイズを取得する必要があります。

だから、あなたは次のように試すことができます:

imageView.snp.makeConstraints { make in 
    // set size 
    make.size.equalTo(CGSize(width: imageWidth, height: imageHeight)) 
    // set position 
    make.top.equalTo(20) 
    make.centerX.equalTo(contentView) 
} 
+0

働いたこと - thksを。 xの位置については、コードをmake.centerX.equalTo(contentView.snp.centerX)に更新しなければなりませんでした。私のレイアウトでは、画像をセルの右側に抱かせて、代わりにmake.right.equalTo(contentView.snp.rightMargin)を使用しました。 – Dennish

関連する問題