繁体   English   中英

UIImageView 忽略宽度/高度约束

[英]UIImageView ignores the width/height constraints

这是我的课:

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”。 例如:

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