简体   繁体   English

“translatesAutoresizingMaskIntoConstraints”实际上是如何工作的?

[英]How does “translatesAutoresizingMaskIntoConstraints” actually work?

I've recently taken up trying to learn how to create iOS applications completely programatically.我最近开始尝试学习如何完全以编程方式创建 iOS 应用程序。 And due to this, one of the first roadblocks I've encountered is that translatesAutoresizingMaskIntoConstraints must usually be set to false in order to set constraints.因此,我遇到的第一个障碍是 translatesAutoresizingMaskIntoConstraints 通常必须设置为 false 才能设置约束。

After doing some research, there are three things that I am pretty sure of.在做了一些研究之后,我很确定三件事。

  1. AutoresizingMasks are how dynamic layouts (layouts that differ based on screen size/orientation) were achieved prior to the introduction of the auto-layout system. AutoresizingMasks 是在引入自动布局系统之前如何实现动态布局(基于屏幕大小/方向的布局)。
  2. AutoresizingMasks are a value that tell a view's superview how to resize it when the superview's bounds change. AutoresizingMasks 是一个值,它告诉视图的超级视图在超级视图的边界发生变化时如何调整它的大小。
  3. translatesAutoresizingMaskIntoConstraints is a boolean value that when set to true (and is always defaulted to true for code-created UIViews), tells the UIKit framework to create constraints that replicate the behavior of the AutoresizingMask property within the auto-layout system. translatesAutoresizingMaskIntoConstraints 是一个 boolean 值,当设置为 true 时(对于代码创建的 UIView,始终默认为 true),告诉 UIKit 框架创建约束,以复制自动布局系统中 AutoresizingMask 属性的行为。

What I don't understand is how exactly these constraints are implemented.我不明白这些约束是如何实现的。 Apple states in their documentation that " the system creates a set of constraints that duplicate the behavior specified by the view's autoresizing mask. This also lets you modify the view's size and location using the view's frame, bounds, or center properties..." Apple 在他们的文档中声明“系统创建了一组约束,这些约束复制了视图的自动调整大小掩码指定的行为。这还允许您使用视图的框架、边界或中心属性来修改视图的大小和位置......”

This is the part that confuses me, and I'm not sure if it's just their wording or my misunderstanding of the topic.这是让我感到困惑的部分,我不确定这只是他们的措辞还是我对这个话题的误解。 The AutoresizingMask is just a value, so it doesn't make sense to be able to create constraints off of it. AutoresizingMask 只是一个值,因此能够从中创建约束是没有意义的。 Do they mean that the automatically generated constraints are actually based off the child view's frame (AKA childView.topAnchor = superview.topAnchor )?它们是否意味着自动生成的约束实际上是基于子视图的框架(AKA childView.topAnchor = superview.topAnchor )? and that the AutoresizingMask's value just determines which of these constraints based on the frame gets set?并且 AutoresizingMask 的值只是确定了基于框架的这些约束中的哪一个被设置?

For example: UIView with frame of (x:0, y:0, width:50, height:50) and AutoresizingMask of "FlexibleBottomMargin".例如:UIView,框架为 (x:0, y:0, width:50, height:50),AutoresizingMask 为“FlexibleBottomMargin”。 Does UIKit just automatically create constraints that place the view where a frame-based layout would have, and then leave out certain constraints (in this case the bottom) to replicate the mask's behavior? UIKit 是否只是自动创建约束,将视图放置在基于框架的布局将具有的位置,然后省略某些约束(在本例中为底部)以复制掩码的行为?

You are correct that the autoresizingMask is interpreted (along with the frame) to determine which constraints to create.您正确地解释了autoresizingMask (与框架一起)以确定要创建哪些约束。

In the example you give the system would create the following constraints -在您给系统的示例中,将创建以下约束 -

  • Width constraint of 50宽度限制为 50
  • Height constraint of 50高度限制为 50
  • Leading, trailing and top constraints to the nearest neighbours, with the fixed distance to those neighbours到最近邻居的前导、尾随和顶部约束,到这些邻居的距离固定
  • Greater than or equal bottom constraint to the nearest neighbour with the distance to that neighbour.大于或等于到最近邻居的底部约束以及与该邻居的距离。

This would result in a 50x50 view that was fixed horizontally and at the top and where the space between the bottom and its neighbour can grow as required.这将产生一个 50x50 的视图,该视图水平固定在顶部,并且底部与其邻居之间的空间可以根据需要增长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM