简体   繁体   English

UIImageView 忽略宽度/高度约束

[英]UIImageView ignores the width/height constraints

This is my class:这是我的课:

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)
    }
}

This is the result:这是结果:

在此处输入图片说明

(orange is my ViewController's background) (橙色是我的 ViewController 的背景)

Why does my image ignores this lines:为什么我的图像忽略了这一行:

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

It has clearly a multiplier of 1.0 in the image.它在图像中显然有 1.0 的乘数。 It should actually take half of the screen, on both height and width as seen in the multiplier (0.5).它实际上应该占据屏幕的一半,在高度和宽度上,如乘数 (0.5) 中所见。 What am I doing wrong?我做错了什么?

Is it just me or is the ".isActive = true" missing on the last two constraints.是只有我还是最后两个约束中缺少“.isActive = true”。 For example:例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM