gpt4 book ai didi

ios - 当我以编程方式添加一个没有约束的 subview 时会发生什么?

转载 作者:行者123 更新时间:2023-11-28 10:09:34 24 4
gpt4 key购买 nike

我经常遇到类似 view.addSubview(myAwesomeTableView) 的事情。

显然,自动布局中没有设置约束。那么系统是如何决定如何以及在哪里放置myAwesomeTableView的。它会简单地匹配父 View 的大小吗?

此外,translatesAutoresizingMaskIntoConstraints 与此有什么关系?

最佳答案

So how does the system determine how and where to place myAwesomeTableView. Will it simply match the size of the parent view?


基本上,它取决于 myAwesomeTableView( subview )frame .添加 subview 时,它会自动添加到 (0,0) 原点,因此 subview 框架将是:

x: 0
y: 0
width: subview width.
height: subview height.

注意,如果 subview 框未定,宽高默认为0。


what does translatesAutoresizingMaskIntoConstraints have to do with this?


改编自translatesAutoresizingMaskIntoConstraints文档:

A Boolean value that determines whether the view’s autoresizing mask is translated into Auto Layout constraints.

这意味着系统会创建一组约束来复制 View 的自动调整大小掩码指定的行为,您将能够通过编辑其帧值直接修改所需的 View 位置(原点)和大小。

注意:

By default, the property is set to true for any view you programmatically create. If you add views in Interface Builder, the system automatically sets this property to false.

在以编程方式向 View 添加约束时,请注意您必须将 translatesAutoresizingMaskIntoConstraints 设置为 false:

myView.translatesAutoresizingMaskIntoConstraints = false

意思是“我自己处理 View 的约束”,否则添加的约束不会被激活。

此外,您可能需要检查:

关于ios - 当我以编程方式添加一个没有约束的 subview 时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49791550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com