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 returnsYES
if the view’s frame is ambiguous. Otherwise, it returnsNO
. 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 havetranslatesAutoresizingMaskIntoConstraints
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 NO
. exerciseAmbiguityInLayout
does not appear to have any effect, and constraintsAffectingLayoutForAxis:
may return additional, unexpected constraints.
作為一個結果,hasambiguouslayout返回第exerciseambiguityinlayout似乎沒有任何效果,和constraintsaffectinglayoutforaxis:可以將額外的,意想不到的約束。