iosautolayoutconstraintsautoresizingmaskadaptive-layout

Is it okay to mix autoresizing mask with autolayout?


I believe using autoresizing for orientational changes would be good while managing the UI with Autolayout.

So many programmers are recommending against both at the same time,But As far as I understand it should be fine.

Because Autoresizingmask is easy to play with.

I think autoresizing mask itself turns into constraints actually I believe

translatesAutoresizingMaskIntoConstraints

Correct me If I am wrong

If its okay or not okay can some explain in depth why ?


Solution

  • As far as I am aware it is absolutely alright to use both autolayout and autoresizingmasks together. What you don't want to do is add autolayout constraints to a view that you are using autoresizing masks to govern layout. A general use case for autoresizing masks is adding a view to a view and wanting it to be pinned top, bottom,leading, and trailing. In that case it is simply

    let pinnedToSuper = UIView(frame: self.view.bounds)
    //all views default to .translatesAutoresizingMaskIntoConstraints if added programmatically
    pinnedToSuper.autoresizingMask = [.flexibleWidth,.flexibleHeight]
    self.view.addSubview(pinnedToSuper)
    

    Notice how much easier this is as opposed to adding each constraint.

    Prior to autolayout autoresizing masks were all iOS had to help with layout. Heads up autoresizing masks are also known as springs and struts. An example use case autoresizing masks break down is if you want a view to maintain a perfect square(or to make a circle) keeping aspect ratio and also resizing with the view in all orientations. In this case you would have to add code in layoutSubviews to resize the view manually based on parent bounds. You can see that this could get tedious especially if you are dodging views that are separately being handled by autolayout. This is probably why it is good to be careful when using both. I tend to use it in simple cases such as adding a view that sticks to the superviews bounds.

    Important distinction when using together.

    You should avoid trying to add autolayout constraints to a view that you are using autoresizing masks to attempt to blend them and achieve a layout because you will likely get conflicting constraints with no real effect. You can however add autolayout constraints to a view that has a subview that is being governed by autoresizing masks and there should not be any issues. This is my typical use case. I hope this helps you with how you might use it.