2016-11-10 73 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) – Sahil

回答

1

您需要在Superview上添加約束。所以,請將其添加到單元格contentView

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

所以這就是問題所在。謝謝你,Sahil! :) –

+0

是的!如果這對你有用,接受答案,這將有助於其他人。 – Sahil

相關問題