我正在使用自调整大小的集合视图单元格,运行时似乎可以正确显示它,但是编译器收到一堆警告,说的都是相同的(运行Xcode 12.1)
collectionViewCell的contentView的translationsAutoresizingMaskIntoConstraints为false,并且缺少对单元的约束,这将导致单元自动调整的性能不合标准。请保持contentView的translationsAutoresizingMaskIntoConstraints为真,否则请在contentView和单元格之间提供约束。
这是我的手机的代码
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(content)
contentView.addSubview(imageView)
content.translatesAutoresizingMaskIntoConstraints = false
imageView.translatesAutoresizingMaskIntoConstraints = false
contentView.translatesAutoresizingMaskIntoConstraints = false
// this is to enable dynamic height for cells
contentView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 40).isActive = true
// content is a UILabel I'm using to display text
content.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
content.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
content.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
content.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
// imageView is an image for content
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
imageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
imageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
layer.borderWidth = 1
layer.borderColor = UIColor.green.cgColor
}
我的代码有什么问题吗?我应该担心这些警告吗?