imstillalive imstillalive - 1 year ago 122
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

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download