debugging Auto Layout:Logical Errors

Logical Errors逻辑错误

Logical errors are simply bugs. Somewhere, you have an assumption that is faulty. Perhaps it’s an assumption about how Auto Layout calculates the views’ frames. Perhaps it’s an assumption about the set of constraints that you’ve created, or the view properties you’ve set. Perhaps it’s an assumption about how the constraints interact to create complex behaviors. Regardless, something somewhere does not quite match your mental model.

逻辑错误只是错误。某处,你有一个假设是错误的。也许这是关于自动布局如何计算视图框架的假设。也许这是关于你创建的约束集的假设,或者是你设置的视图属性。也许这是一个假设的约束如何相互作用,创造复杂的行为。无论如何,某个地方的东西与你的心智模型不太相符。

Logical errors are the hardest to find. After you eliminate all other possibilities, whatever remains, however improbable, must be a logical error. However, even after you’ve determined that you have a bug, you must still discover where, exactly, the faulty assumption lies.

逻辑错误是最难找到。在排除所有其他可能性之后,无论剩下什么,无论多么不可能,都必须是一个逻辑错误。然而,即使你已经确定你有一个bug,你必须仍然发现哪里,确切地说,错误的假设谎言。

There are no tools or step-by-step instructions here. Fixing logical errors typically involves experiments and iterative tests, both to identify the problem and to figure out how to fix it. There are, however, a few suggestions that may help:

这里没有工具或一步一步的说明。固定逻辑错误通常涉及实验和迭代测试,既要找出问题,又要找出如何解决它。有,但是,一些建议,可能有助于:

  • Review the existing constraints. Make sure you haven’t missed any constraints or accidentally added unwanted constraints. Make sure all the constraints are attached to the correct items and attributes.回顾现有约束。确保您没有错过任何约束或意外添加不必要的约束。确保所有约束都附在正确的项目和属性上。

  • Double-check the view frames. Make sure nothing is getting unexpectedly stretched or shrunk.双检查视图帧。确保没有什么意外的拉伸或缩小。

    This is particularly important for views with invisible backgrounds, like labels or buttons. It may not be obvious when these items are unexpectedly resized.这是特别重要的视图与无形的背景,如标签或按钮。它可能不是很明显时,这些项目都是出人意料的调整。

    One symptom of resizing is that baseline-aligned views no longer line up properly. This is because the baseline alignment works only when the view is displayed at its intrinsic content height. If you stretch or shrink the view vertically, the text mysteriously appears in the wrong location.调整大小的一个症状是基线对齐视图不再正确排列。这是因为基线对齐仅在视图以其内在内容高度显示时生效。如果垂直拉伸或缩小视图,则文本会出现在错误的位置。

  • If a control should always match its intrinsic content size, give it a very high content-hugging and compression-resistance priority (for example, 999). 

    如果控件应该始终匹配其内在内容的大小,给它一个非常高的内容拥抱和压缩优先权(例如,999)。

  • Look for any assumptions that you’re making about the layout, and add explicit constraints to make sure those assumptions are true.寻找关于布局的任何假设,并添加显式约束以确保这些假设是正确的。

    Remember, unsatisfiable layouts are generally the easiest problems to find and fix. Add additional constraints until you have a conflict, then examine and fix the conflict.

    记住,不可满足的布局一般是最容易发现问题和解决问题。添加额外的约束,直到你有冲突,然后检查和修复冲突。

  • Try to understand why the given constraints are producing the results that you see. If you understand it, you’re well on the way to fixing it.试着理解为什么给定的约束产生你所看到的结果。如果你明白了,那么你就在修复它的道路上。

  • Experiment with alternative constraints. Auto Layout typically gives you a number of different solutions for the same problem. Trying an different approach may fix the problem or at least make it easier to spot the mistake.替代约束实验。自动布局通常为同一问题提供多个不同的解决方案。尝试不同的方法可以解决这个问题,或者至少能让你更容易发现错误。

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