gpt4 book ai didi

ios - 我向哪个 super View 添加约束重要吗?

转载 作者:行者123 更新时间:2023-11-29 03:42:46 26 4
gpt4 key购买 nike

假设我有 3 次观看

 A|----------|
| B|-----| |
| | C ▢ | |
| |_____| |
|__________|

如果我有涉及 View B 和 View C 的约束。

documentation建议添加到 View C 是非法的:

The constraint must involve only views that are within scope of the receiving view. Specifically, any views involved must be either the receiving view itself, or a subview of the receiving view. Constraints that are added to a view are said to be held by that view. The coordinate system used when evaluating the constraint is the coordinate system of the view that holds the constraint.

  • 将其添加到 View A 还是 View B 有关系吗?
  • 添加到一个 super View 而不是另一个 super View 有什么影响吗?
  • 是否有任何便利性影响? (例如,如果所有约束都集中在一个地方,管理起来会更容易吗?)

最佳答案

  • 将其添加到 View A 还是 View B 并不重要,只要它们满足您在文档中引用的要求即可。
  • 据我所知,没有任何影响,无论哪种方式布局都是相同的。 根据中的注释,约束不一定会添加到您显式添加它们的 View 中UIView 头文件(参见下面的摘录)它们通常安装在约束中涉及的 View 最近的祖先处(并且 View 被认为是它自己的祖先)。
  • 这两种方式本质上都不比另一种更方便(除非您没有对 A 的引用,在这种情况下,将其添加到 B 显然更方便)。这取决于您的应用程序的设置(您有哪些导出/引用等)。 鉴于约束没有安装在您显式添加它们的地方,这两种方法都不会更方便。不过,我认为将它们添加到系统最终安装它们的位置会稍微更有效。

编辑:

此注释来自 UIView.h,位于用于安装约束的方法之上:

/* A constraint is typically installed on the closest common ancestor of the views involved in the constraint. 
It is required that a constraint be installed on _a_ common ancestor of every view involved. The numbers in a constraint are interpreted in the coordinate system of the view it is installed on. A view is considered to be an ancestor of itself.
*/

关于ios - 我向哪个 super View 添加约束重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18176446/

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