Search code examples
iosswiftnslayoutconstraint

Proper way to set height of imageView


When im working in Swift on NSLayoutConstraint and I want set height of imageView what is proper way to do that:

1) get view.frame.height CGFloat and use

let viewHeight = view.frame.height
customNSLayoputConstraints.append(imageView.heightAnchor.constraint(equalToConstant: viewHeight / 5))

2) or use view.heightAnchor

customNSLayoutConstraints.append(imageView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.2))

Thanks for answers.


Solution

  • Better option will be 2nd one. Its true what @BrunoRocha wrote in comment, constraints will update automatically when you get height from view.heightAnchor instead from frame.