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.替代約束實驗。自動布局通常為同一問題提供多個不同的解決方案。嘗試不同的方法可以解決這個問題,或者至少能讓你更容易發現錯誤。