简体   繁体   中英

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)

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. It should actually take half of the screen, on both height and width as seen in the multiplier (0.5). What am I doing wrong?

Is it just me or is the ".isActive = true" missing on the last two constraints. For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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