swift - Collection View 中的 ImageView 必须居中且宽高比适合快速

标签 swift xcode swift4

我尝试在 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/

相关文章:

swift - 顶级 Bool 编码为数字属性列表片段。属性列表编码器

ios - 允许用户删除文本字段文本但限制最大文本长度

c++ - 为 Mac 10.9 10.11 下编译

iphone - 在Xcode 4.5中构建AVFoundation.framework时出现编译错误

iphone - 从 XIB 文件过渡到 Storyboard

ios - 静态让 sharedInstance 无法从 Objective-C 代码访问

ios - 回到第一个 View Controller 时无法停止音乐

json - 解码 JSON 对象(发送带参数的 GET 请求) - Swift 4 Decodable

swift - 不使用 cocoapods 或其他外部 API 读取条形码图像

ios - 无法使用类型为 'Double' 的参数列表调用类型为 '(Range<Double>)' 的初始值设定项