因此,我具有混合大小的自定义单元格和固定大小的单元格的集合视图。 它适合自我调整大小的对象,但是我无法让固定大小的对象保持固定大小。这是我的工作:
// I have a viewContainer inside the cell's contentView
let viewContainer = UIView()
viewContainer.backgroundColor = .red
viewContainer.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(viewContainer)
// This viewContainer has a fixed width/height and is achored to the top, left, right, bottom of the contentView
viewContainer.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
viewContainer.leftAnchor.constraint(equalTo: self.contentView.leftAnchor).isActive = true
viewContainer.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
viewContainer.rightAnchor.constraint(equalTo: self.contentView.rightAnchor).isActive = true
viewContainer.heightAnchor.constraint(equalToConstant: FIXED_HEIGHT).isActive = true
viewContainer.widthAnchor.constraint(equalToConstant: FIXED_WIDTH).isActive = true
// Then I got my ImageView (property of my cell), as a viewContainer subview
self.photoImageView.translatesAutoresizingMaskIntoConstraints = false
self.photoImageView.contentMode = .scaleAspectFill
viewContainer.addSubview(self.photoImageView)
// It has a fixed width/height and is achored to the top, left, right, bottom of the viewContainer
self.photoImageView.topAnchor.constraint(equalTo: viewContainer.topAnchor).isActive = true
self.photoImageView.leftAnchor.constraint(equalTo: viewContainer.leftAnchor).isActive = true
self.photoImageView.bottomAnchor.constraint(equalTo: viewContainer.bottomAnchor).isActive = true
self.photoImageView.rightAnchor.constraint(equalTo: viewContainer.rightAnchor).isActive = true
self.photoImageView.heightAnchor.constraint(equalToConstant: FIXED_HEIGHT).isActive = true
self.photoImageView.widthAnchor.constraint(equalToConstant: FIXED_WIDTH).isActive = true
以某种方式,当我有图像时(从图像选择器中,在委托中具有简单的重新加载数据),将调整单元格的大小,即图像的大小:
但是在collectionView上快速滚动之后,它看起来还不错:
我厌倦了在heightAnchor和withAnchor UILayoutPriority(999.0)
上添加高优先级,但这似乎无济于事。我找不到找到想要的大小以及该单元格图像大小的方法。我觉得它与图像视图中的图像有关,但我不知道它的操作方式和原因。
有人可以帮我吗?
预先感谢您的回答。
编辑: 我还尝试创建一个UIImageView子类并重写internalContentSize以使其返回我想要的大小,但仍然没有成功:/
答案 0 :(得分:0)
不确定这是最佳解决方案,但我结束了以999优先级在单元格的contentView中添加宽度/高度约束。我仍然有自动版式警告。但这似乎“行得通”。