gpt4 book ai didi

ios - 无法在 UICollectionView 单元格和单元格 subview 之间添加约束

转载 作者:行者123 更新时间:2023-11-28 16:04:14 24 4
gpt4 key购买 nike

在我运行我的应用程序后,它崩溃了:

Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal.

我的图像是单元格的 subview 。

我做错了什么?

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)

最佳答案

您需要在 Superview 上添加约束。所以,将它添加到单元格 contentView 上。

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

关于ios - 无法在 UICollectionView 单元格和单元格 subview 之间添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40527275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com