J. Doe
J. Doe

Reputation: 13033

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:

enter image description here

(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?

Upvotes: 2

Views: 1205

Answers (1)

Raj
Raj

Reputation: 346

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

Upvotes: 7

Related Questions