我尝试在 Collection View 中的 imageView 中以中心和宽高比配合,但不在 Collection View 中执行任何操作。请任何人帮助我
imageproduct.center = self.viewImage.center
imageproduct.contentMode = .scaleAspectFit
self.viewImage.addSubview(imageproduct)
最佳答案
您是否尝试过在添加到 subview 后为其提供 centerX 和 centerY 约束?
imageproduct.translatesAutoresizingMaskIntoConstraints = false
imageproduct.centerXAnchor.constraint(equalTo: self.viewImage.centerXAnchor).isActive = true
imageproduct.centerYAnchor.constraint(equalTo: self.viewImage.centerYAnchor).isActive = true
还要确保您的 self.viewImage 也受到适当的约束。
关于swift - Collection View 中的 ImageView 必须居中且宽高比适合快速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58606249/