这是我的课:
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)
}
}
结果如下:
(橙色是我的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”?