这是我的班级:
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)所示。我做错了什么?
答案 0 :(得分:4)
仅仅是我还是最后两个限制中缺少“.isActive = true”?