Pretty often I find myself writing these lines of code:
myView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
myView.topAnchor.constraint(equalTo: myView.superview.topAnchor),
myView.bottomAnchor.constraint(equalTo: myView.superview.bottomAnchor),
myView.leadingAnchor.constraint(equalTo: myView.superview.leadingAnchor),
myView.trailingAnchor.constraint(equalTo: myView.superview.trailingAnchor)
])
So I'm thinking to write an extension. Something like this:
extension UIView {
func bindFrameToSuperviewBounds() {
self.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.topAnchor.constraint(equalTo: self.superview.topAnchor),
self.bottomAnchor.constraint(equalTo: self.superview.bottomAnchor),
self.leadingAnchor.constraint(equalTo: self.superview.leadingAnchor),
self.trailingAnchor.constraint(equalTo: self.superview.trailingAnchor)
])
}
}
My questions:
Maybe some built-in function (or technique) like this already exists, I just don't know about it (I've googled a lot, though)
Isn't (in theory) this code equivalent to:
myView.translatesAutoresizingMaskIntoConstraints = true myView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
Why in theory? Because in practice it's definitely not equivalent. This alternative technique didn't ever give me a result I expected to see. The result is pretty much unpredictable.
Answering comments:
Where:
Green (outer) rectangle is a containerView (UIView).
Purple (inner) rectangle is a UIStackView, which I'm inserting.
As you can see, constraint approach works great.
Next one is a result of autoresizing mask approach:
Why there are three pictures?
Because the result is different with every new launch of the application!