Is it okay to mix autoresizing mask with autolayout?

时光总嘲笑我的痴心妄想 提交于 2020-01-13 20:42:11

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/42661539/is-it-okay-to-mix-autoresizing-mask-with-autolayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!