2016-11-10 8 views
0

私のアプリケーションを実行するとクラッシュし、その理由は次のようになります。 "制約をビューにインストールできません。それは違法だ」 私の画像はセルのサブビューですが、何が間違っていますか?UICollectionViewセルとセルのサブビューの間に制約を追加できません

  let image = UIImageView(frame: CGRect(x: 0, y: 0, width: 300, height: 50)) 
      image.image = UIImage.init(named: "Bitmap") 

      cell.contentView.addSubview(image) 

      let bottomConstr = NSLayoutConstraint(item: image, attribute: .bottom, relatedBy: .equal, toItem: cell.contentView, attribute: .bottom, multiplier: 1, constant: 0) 
      //let leftConstr = NSLayoutConstraint(item: image, attribute: .leading, relatedBy: .equal, toItem: cell.contentView, attribute: .leading, multiplier: 1, constant: 0) 
      //let rightConstr = NSLayoutConstraint(item: image, attribute: .trailing, relatedBy: .equal, toItem: cell.contentView, attribute: .trailing, multiplier: 1, constant: 0) 
      let heightConstr = NSLayoutConstraint(item: image, attribute: .height, relatedBy: .equal, toItem: nil , attribute: .notAnAttribute, multiplier: 1, constant: 10) 

      image.addConstraint(bottomConstr) 
      //image.addConstraint(leftConstr) 
      //image.addConstraint(rightConstr) 
      image.addConstraint(heightConstr) 
+0

cell.contentView.addConstraint(bottomConstr)のようにcontentViewに制約を追加する必要があります。 – Sahil

答えて

1

Superviewに制約を追加する必要があります。セルcontentViewに追加してください。

cell.contentView.addConstraint(bottomConstr) 
    cell.contentView.addConstraint(heightConstr) 
+0

これがポイントです。ありがとう、サヒル! :) –

+0

はい!これがあなたのために働いた場合は、他の人に役立つように答えを受け入れます。 – Sahil

関連する問題