imstillalive imstillalive - 2 months ago 34
Swift Question

iOS - Understanding NSLayoutConstraint multiplier

I'm trying to set the height of a view according to my View Controller's height. This is my code.

self.view.topAnchor.constraint(equalTo: someView.topAnchor),
self.view.leftAnchor.constraint(equalTo: someView.leftAnchor),
self.view.rightAnchor.constraint(equalTo: someView.rightAnchor),
self.view.heightAnchor.constraint(equalTo: someView.heightAnchor, multiplier: 20.0, constant: 0.0)

I want
's height to be 20% of the View Controller's total height. The result I got now is incorrect.
's height doesn't change when ever I change the multiplier.

What am I doing wrong?


0.2 shows incorrect result. Someview appeared larger than View Controller


You are reading the parameter incorrectly. 20 means you want view's height to be 20 times bigger than someView's height. What you want is view to be 20% of someView's hieght, so you need to multiply by .2.

    self.view.heightAnchor.constraint(equalTo: someView.heightAnchor, multiplier: 0.2, constant: 0.0)