Debugging Auto Layout:Ambiguous Layouts

Ambiguous Layouts 模棱两可的布局

Ambiguous layouts occur when the system of constraints has two or more valid solutions. There are two main causes:当约束系统有两个或多个有效解时,会出现不规则的布局。主要有两个原因:

The layout needs additional constraints to uniquely specify the position and location of every view.布局需要额外的约束来唯一指定每个视图的位置和位置。

  • After you determine which views are ambiguous, just add constraints to uniquely specify both the view’s position and its size.在确定哪些视图是不明确的,只需添加约束,以唯一指定视图的位置和大小。

  • The layout has conflicting optional constraints with the same priority, and the system does not know which constraint it should break.布局有相同的优先级的冲突的可选约束,系统不知道它应该打破哪些约束。

    Here, you need to tell the system which constraint it should break, by changing the priorities so that they are no longer equal. The system breaks the constraint having the lowest priority first.在这里,您需要告诉系统它应该打破的约束,通过改变优先级,使他们不再平等。系统首先中断具有最低优先级的约束。

Detecting Ambiguous Layouts检测模糊的布局

As with unsatisfiable layouts, Interface Builder can often detect, and offer suggestions to fix, ambiguous layouts at design time. These ambiguities appear as warnings in the issues navigator, errors in the document outline, and red lines in the canvas. For more information, see Identifying Unsatisfiable Constraints.

与不可满足的布局,界面生成器可以经常检测,并提供建议来解决,模棱两可的布局在设计时。这些歧义出现在问题导航器中的警告、文档大纲中的错误以及画布中的红线。更多信息,见识别不可满足的约束条件。

As with unsatisfiable layouts, Interface Builder cannot detect all possible ambiguities. Many errors can be found only through testing.

与不可满足的布局,界面生成器无法检测出所有可能的歧义。许多错误只能通过测试才能找到。

When an ambiguous layout occurs at runtime, Auto Layout chooses one of the possible solutions to use. This means the layout may or may not appear as you expect. Furthermore, there are no warnings written to the console, and there is no way to set a breakpoint for ambiguous layouts.

当运行时出现模糊布局时,自动布局选择使用的可能的解决方案之一。这意味着布局可能会或可能不会出现如你所期望的那样。此外,还没有写入控制台的警告,也没有办法为不明确的布局设置断点。

As a result, ambiguous layouts are often harder to detect and identify than unsatisfiable layouts. Even if the ambiguity does have an obvious, visible effect, it can be hard to determine whether the error is due to ambiguity or to an error in your layout logic.

作为一个结果,模棱两可的布局往往是难以检测和识别比永无止境的布局。即使歧义确实有明显的、可见的效果,也很难确定错误是否是由于布局逻辑中的歧义或错误引起的。

Fortunately, there are a few methods you can call to help identify ambiguous layouts. All of these methods should be used only for debugging. Set a breakpoint somewhere where you can access the view hierarchy, and then call one of the following methods from the console:

幸运的是,有几个方法可以调用以帮助识别模糊布局。所有这些方法只能用于调试。在可以访问视图层次结构的地方设置断点,然后从控制台调用下列方法之一

  • hasAmbiguousLayout. Available for both iOS and OS X. Call this method on a misplaced view. It returns YES if the view’s frame is ambiguous. Otherwise, it returns NO. hasambiguouslayout。可用于iOS和OS X调用这种方法在错位的观点。如果视图的框架是模糊的,它返回。否则,它返回。

  • exerciseAmbiguityInLayout. Available for both iOS and OS X. Call this method on a view with ambiguous layout. This will toggle the system between the possible valid solutions. exerciseambiguityinlayout。可用于iOS和OS X调用此方法的视图与布局暧昧。这将切换系统之间的可能有效的解决方案。

  • constraintsAffectingLayoutForAxis:. Available for iOS. Call this method on a view. It returns an array of all the constraints affecting that view along the specified axis.constraintsaffectinglayoutforaxis:。可供iOS。在视图上调用此方法。它返回一个数组的所有约束影响该视图沿指定的轴。

  • constraintsAffectingLayoutForOrientation:. Available for OS X. Call this method on a view. It returns an array of all the constraints affecting that view along the specified orientation.constraintsaffectinglayoutfororientation:。可用的OS x调用此方法的视图。它返回沿指定方向影响该视图的所有约束的数组。

  • _autolayoutTrace. Available as a private method in iOS. Call this method on a view. It returns a string with diagnostic information about the entire view hierarchy containing that view. Ambiguous views are labeled, and so are views that have translatesAutoresizingMaskIntoConstraints set to YES._autolayouttrace。在iOS中作为一个私有的方法可用。在视图上调用此方法。它返回包含包含该视图的整个视图层次结构的诊断信息的字符串。模棱两可的观点进行标记的,所以有translatesautoresizingmaskintoconstraints设置为“是”的观点。

You may need to use Objective-C syntax when running these commands in the console. For example, after the breakpoint halts execution, type call [self.myView exerciseAmbiguityInLayout]into the console window to call the exerciseAmbiguityInLayout method on the myView object. Similarly, type po [self.myView autolayoutTrace] to print out diagnostic information about the view hierarchy containing myView.

您可能需要使用Objective-C语法时运行这些命令在控制台。例如,在断点处停止执行,调用类型[ self.myview exerciseambiguityinlayout ]到控制台窗口呼吁MyView对象的exerciseambiguityinlayout方法。同样,型坡[ self.myview autolayouttrace ]打印出含MyView视图层次的诊断信息。

NOTE

Be sure to fix any issues found by Interface Builder before running the diagnostic methods listed above. Interface Builder attempts to repair any errors it finds. This means that if it finds an ambiguous layout, it adds constraints so that the layout is no longer ambiguous.

在运行上面列出的诊断方法之前,请务必修复界面生成器所发现的任何问题。接口生成器试图修复发现的任何错误。这意味着,如果它找到一个不明确的布局,它增加了约束,使布局不再是模棱两可的。

As a result, hasAmbiguousLayout returns NOexerciseAmbiguityInLayout does not appear to have any effect, and constraintsAffectingLayoutForAxis: may return additional, unexpected constraints.

作为一个结果,hasambiguouslayout返回第exerciseambiguityinlayout似乎没有任何效果,和constraintsaffectinglayoutforaxis:可以将额外的,意想不到的约束。

posted @ 2017-03-24 12:35  small英  阅读(2293)  评论(0编辑  收藏  举报