将自动调整大小的蒙版与自动布局混合可以吗? [英] Is it okay to mix autoresizing mask with autolayout?

查看:93
本文介绍了将自动调整大小的蒙版与自动布局混合可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信使用自动调整大小进行方向更改会很好,同时可以通过自动布局管理UI.

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

将自动调整大小遮罩转换为约束

纠正我,如果我错了

如果可以的话,可以深入解释为什么吗?

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

推荐答案

据我所知,将autolayout和autoresizingmasks一起使用是绝对可以的.您不想执行的操作是将自动布局约束添加到使用自动调整大小蒙版来控制布局的视图中.自动调整蒙版大小的一般用例是将视图添加到视图,并希望将其固定在顶部,底部,前导和尾随.在这种情况下,

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.

在自动布局之前,自动调整大小的蒙版都是iOS必须帮助进行布局的.平视自动调整大小的蒙版也称为弹簧和支柱.自动调整大小蒙版分解的一个示例用例是,如果您希望视图保持完美的正方形(或制作一个圆形),并保持宽高比,并且还希望在所有方向上调整视图的大小.在这种情况下,您将不得不在layoutSubviews中添加代码以根据父边界手动调整视图的大小.您会看到这可能变得很乏味,尤其是当您避开自动布局分别处理的视图时.这可能是为什么在同时使用两者时要小心一点的原因.我倾向于在简单的情况下使用它,例如添加一个坚持超级视图范围的视图.

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.

这篇关于将自动调整大小的蒙版与自动布局混合可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆