【问题标题】:Does it matter which superview I add constraints to?我向哪个超级视图添加约束是否重要?
【发布时间】:2013-08-11 20:39:02
【问题描述】:

假设我有 3 个视图

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

如果我有一个涉及视图 B 和视图 C 的约束。

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

约束必须只涉及接收视图范围内的视图。具体来说,涉及的任何视图必须是接收视图本身,或者是接收视图的子视图。添加到视图的约束被称为由该视图持有。评估约束时使用的坐标系是持有该约束的视图的坐标系。

  • 将其添加到视图 A 或视图 B 是否重要?
  • 添加到一个超级视图而不是另一个有什么影响吗?
  • 是否有任何方便的含义? (例如,如果所有约束都在一个地方,会更容易管理吗?)

【问题讨论】:

    标签: ios cocoa-touch autolayout nslayoutconstraint


    【解决方案1】:
    • 将其添加到视图 A 或视图 B 都没有关系,只要它们满足您在文档中引用的要求即可。
    • 据我所知,布局没有任何影响。 根据 UIView 标头中的 cmets,约束不一定添加到您明确添加到的视图中文件(参见下面的摘录),它们通常安装在与约束中涉及的视图最近的祖先处(并且视图被认为是它自己的祖先)。
    • 这两种方法本质上都比另一种更方便(除非您没有对 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.
     */
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 2013-09-28
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多