簡體   English   中英

iOS“無法同時滿足約束條件”

[英]iOS “Unable to simultaneously satisfy constraints”

我將使用以下表示法來解釋已入侵的視圖:

  • {V} –“超級視圖”,即根控制器的主視圖
  • {Q} –屏幕中央的一個矩形,用作測驗
  • {W} – {Q}上方的白條

在iPad-Air2模擬器上運行時,我得到以下輸出:

2016-11-03 08:09:07.700117 MyApp[16645:6976134] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x60000009d600 h=--& v=--& QuizButtons:0x7fcb8e521830.width == 717   (active)>",
    "<NSLayoutConstraint:0x608000281fe0 H:|-(0)-[ImgWhiteBar:0x7fcb8e525020]   (active, names: '|':ViewTestVC:0x7fcb8e639b30 )>",
    "<NSLayoutConstraint:0x6080002820d0 H:[ImgWhiteBar:0x7fcb8e525020]-(0)-|   (active, names: '|':ViewTestVC:0x7fcb8e639b30 )>",
    "<NSLayoutConstraint:0x608000282210 ImgWhiteBar:0x7fcb8e525020.width == 1.07143*QuizButtons:0x7fcb8e521830.width   (active)>",
    "<NSLayoutConstraint:0x60000009b9e0 'UIView-Encapsulated-Layout-Width' ViewTestVC:0x7fcb8e639b30.width == 768   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6080002820d0 H:[ImgWhiteBar:0x7fcb8e525020]-(0)-|   (active, names: '|':ViewTestVC:0x7fcb8e639b30 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我對記錄的約束的解釋是:

  1. {Q}:默認<h=--& v=--&> ,寬度= 717
  2. Horiz:{V.lead} -0- {W}
  3. Horiz:{W} -0- {V.trail}
  4. {W.width} = 1.07 * {Q.width}
  5. {V.width} = 768

也許更簡單

  1. {Q.width}必須為717
  2. {W.width}必須為768.21531
  3. {W}必須碰到{V}的兩面
  4. {V.width} = 768

問題:

  • 我的解釋正確嗎?
  • 這里有什么問題? 是由於768.2與768的不准確性? 如果是,誰告訴Xcode使用717? 我告訴{Q.width}為{W.width} / [14:15]

在這里將不勝感激!

編輯

這是三個約束:

在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明

沖突約束的列表包括h=--& v=--& ,這是默認的非自動布局形式。

我試圖使用自動布局獲取初始大小和位置,然后嘗試通過設置translatesAutoresizingMaskIntoConstraints = YES來關閉自動布局。 我在Apple開發人員論壇上得到了一個答案,說在這種情況下,我需要刪除視圖的所有約束,可能是通過從超級視圖中刪除並添加回來。

小費

在研究此錯誤時,我發現了一種使約束沖突日志更易於理解的方法。 問題是視圖顯示為匿名,僅指定類而不指定名稱。

為了使您的視圖可識別 ,請打開您的.m文件之一,並為要標識的每個視圖添加一個新類,如下所示:

@interface ImgWhiteBar: UIImageView
@end
@implementation ImgWhiteBar
@end    

@interface Spacer1: UIView
@end
@implementation Spacer1
@end

之后,在InterfaceBuilder中,選擇每個視圖,然后在右側的“ Identity Inspector”中,將通用類(UIVIew,UIImageView等)修改為您剛創建的類之一。

現在再次運行,然后運行Abracadabra-所有視圖現在都通過其自定義類進行標識,從而使您能夠了解那里發生的情況。

享受有趣的調試約束!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM