gpt4 book ai didi

ios - UIImageView 忽略宽度/高度约束

转载 作者:搜寻专家 更新时间:2023-11-01 06:02:13 26 4
gpt4 key购买 nike

这是我的课:

class Some: UIView{
override init(frame: CGRect) {
super.init(frame: frame)
load()

}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
load()
}

private func load(){
let someImage = UIImageView()
someImage.translatesAutoresizingMaskIntoConstraints = false
someImage.image = UIImage(named: "Test")
self.addSubview(someImage)
someImage.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
someImage.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
someImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5)
someImage.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.5)
}
}

这是结果:

enter image description here

(橙色是我的 ViewController 的背景)

为什么我的图片忽略了这行:

someImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5)
someImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5)

它在图像中显然有一个 1.0 的乘数。它实际上应该占据屏幕的一半,如乘数 (0.5) 中所示的高度和宽度。我做错了什么?

最佳答案

是只有我还是最后两个约束中缺少“.isActive = true”。例如:

someImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5).isActive = true
someImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.5).isActive = true

关于ios - UIImageView 忽略宽度/高度约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46248429/

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