簡體   English   中英

無法同時滿足約束,將嘗試通過打破約束來恢復

[英]Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint

下面是我在調試區收到的錯誤消息。 它運行良好,除了我收到此錯誤外,沒有任何問題。 這會阻止蘋果接受該應用程序嗎? 我如何解決它?

2012-07-26 01:58:18.621 Rolo[33597:11303] 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:0x887d630 h=--& v=--& V:[UIButtonLabel:0x886ed80(19)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x887d5f0 h=--& v=--& UIButtonLabel:0x886ed80.midY == + 37.5>",
    "<NSAutoresizingMaskLayoutConstraint:0x887b4b0 h=--& v=--& V:[UIButtonLabel:0x72bb9b0(19)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x887b470 h=--& v=--& UIButtonLabel:0x72bb9b0.midY == - 0.5>",
    "<NSLayoutConstraint:0x72bf860 V:[UILabel:0x72bf7c0(17)]>",
    "<NSLayoutConstraint:0x72c2430 UILabel:0x72bfad0.top == UILabel:0x72bf7c0.top>",
    "<NSLayoutConstraint:0x72c2370 UILabel:0x72c0270.top == UILabel:0x72bfad0.top>",
    "<NSLayoutConstraint:0x72c22b0 V:[UILabel:0x72bf7c0]-(NSSpace(8))-[UIButton:0x886efe0]>",
    "<NSLayoutConstraint:0x72c15b0 V:[UILabel:0x72c0270]-(NSSpace(8))-[UIRoundedRectButton:0x72bbc10]>",
    "<NSLayoutConstraint:0x72c1570 UIRoundedRectButton:0x72bbc10.baseline == UIRoundedRectButton:0x7571170.baseline>",
    "<NSLayoutConstraint:0x72c21f0 UIRoundedRectButton:0x7571170.top == UIButton:0x886efe0.top>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x72bf860 V:[UILabel:0x72bf7c0(17)]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

我建議調試並找到哪個約束是“你不想要的” 假設您有以下問題:

在此處輸入圖片說明

問題始終是如何找到以下約束和視圖。

有兩種解決方案如何做到這一點:

  1. DEBUG VIEW HIERARCHY (不推薦這種方式)

由於您知道在哪里可以找到意外約束 (PBOUserWorkDayHeaderView),因此有一種方法可以很好地做到這一點。 讓我們在紅色矩形中找到UIViewNSLayoutConstraint 因為我們知道他們在內存中id,所以很容易。

  • 使用Debug View Hierarchy停止應用程序:

在此處輸入圖片說明

  • 找到合適的 UIView:

在此處輸入圖片說明

  • 接下來就是找到我們關心的NSLayoutConstraint:

在此處輸入圖片說明

如您所見,內存指針是相同的。 所以我們知道現在發生了什么。 此外,您可以在視圖層次結構中找到NSLayoutConstraint 由於它是在 View 中選擇的,因此它也在 Navigator 中選擇了。

在此處輸入圖片說明

如果您需要,您也可以使用地址指針在控制台上打印它:

(lldb) po 0x17dce920
<UIView: 0x17dce920; frame = (10 30; 300 24.5); autoresize = RM+BM; layer = <CALayer: 0x17dce9b0>>

您可以對調試器指向您的每個約束執行相同的操作:-) 現在您決定如何處理它。

  1. 更好地打印(我真的推薦這種方式,這是 Xcode 7)

    • 為視圖中的每個約束設置唯一標識符:

在此處輸入圖片說明

  • NSLayoutConstraint創建簡單的擴展:

斯威夫特

extension NSLayoutConstraint {

    override public var description: String {
        let id = identifier ?? ""
        return "id: \(id), constant: \(constant)" //you may print whatever you want here
    }
}

目標-C

@interface NSLayoutConstraint (Description)

@end

@implementation NSLayoutConstraint (Description)

-(NSString *)description {
    return [NSString stringWithFormat:@"id: %@, constant: %f", self.identifier, self.constant];
}

@end
  • 再次構建它,現在你有更多可讀的輸出給你:

在此處輸入圖片說明

  • 獲得id您可以在Find Navigator 中簡單地點擊它:

在此處輸入圖片說明

  • 並快速找到它:

在此處輸入圖片說明

如何簡單地解決這個問題?

  • 嘗試將優先級更改為999以破壞約束。

您遇到的問題是 NSAutoresizingMaskLayoutConstraints 不應該在那里。 這是彈簧和支柱的舊系統。 要擺脫它,請在您要約束的每個視圖上運行此方法:

[view setTranslatesAutoresizingMaskIntoConstraints:NO];

請注意,不要在同一方向和類型上使用多個約束。

例如:尾隨的垂直約束 = 15,另一個是 >= 10。

有時,Xcode 會創建一些您沒有注意到的約束 你必須擺脫多余的約束,日志警告肯定會消失。

在此處輸入圖片說明

另外,您可以直接從日志中讀取和檢測某些原因:

NSLayoutConstraint:0xa338390 V:|-(15)-[UILabel:0xa331260] (Names: '|':UILabel:0xa330270 )>

這我們可以理解為 UILabel 約束中的問題,它是前導垂直約束,長度為 15pt。

NSLayoutConstraint:0x859ab20 H:-(13)-|[UIView:0x85a8fb0]...

這將是尾隨水平約束等。

我拋出了很多這樣的異常,我發現解決它們的最快和最簡單的方法是在異常中找到唯一值,然后我在故事板源代碼中搜索這些值。 這幫助我找到了導致問題的實際視圖和約束(我在所有視圖上使用了有意義的 userLabels,這使得跟蹤約束和視圖變得更加容易)...

因此,使用上述例外情況,我將在 xcode(或其他編輯器)中將故事板作為“源代碼”打開並尋找我能找到的東西......

<NSLayoutConstraint:0x72bf860 V:[UILabel:0x72bf7c0(17)]> 

.. 這看起來像是 UILabel 上的垂直 (V) 約束,值為 (17)。

查看異常我也發現

<NSLayoutConstraint:0x72c22b0 V:[UILabel:0x72bf7c0]-(NSSpace(8))-[UIButton:0x886efe0]>

看起來 UILabel(0x72bf7c0) 接近 UIButton(0x886efe0),有一些垂直間距 (8)..

希望這足以讓我在故事板源代碼中找到特定的視圖(可能是通過最初搜索文本中的“17”),或者至少是一些可能的候選者。 從那里我應該能夠實際找出故事板中的哪些視圖,這將使識別問題變得更加容易(尋找“重復的”固定或與大小限制沖突的固定)。

我很難弄清楚是什么限制導致了這個錯誤。 這是一個更簡單的方法。

我正在使用 Xcode 6.1.1

  1. “Command + A”選擇所有 UILabels、UIImages 等。
  2. 單擊 Editor -> Pin > (Select...) to Superview
  3. 再次單擊 Editor -> Resolve Auto Layout Issues -> Add Missing Constraints or Reset to Suggested Constraints。 這取決於你的情況。

我遇到這個問題是因為我的.xib文件使用了自動布局。

在文件檢查器中,第一個選項卡。 取消勾選“使用自動布局”解決了這個問題。

自動布局文件檢查器

這是我的經驗和解決方案。 我沒有碰代碼

  1. 選擇視圖(UILabel、UIImage 等)
  2. 編輯器 > 固定 >(選擇...)到超級視圖
  3. 編輯器 > 解決自動布局問題 > 添加缺少的約束

快速使用此代碼

view.translatesAutoresizingMaskIntoConstraints = false

我遵循了每個搜索查詢中的 SO 問題和答案。 但它們都與特定的相關。

基本上,我的意思是在你准備寫下一種格式(可能是一個簡單的格式)之前,它會給你一個警告。

從 iOS 8.0 開始,默認視圖是大小類。 即使您禁用尺寸類,它仍然會包含一些自動布局約束。

因此,如果您打算使用 VFL 通過代碼設置約束。 那么你必須照顧一個線下。

// Remove constraints if any.
[self.view removeConstraints:self.view.constraints];

我在 SO 中進行了大量搜索,但解決方案在於Apple Sample Code

因此,您必須在計划添加新約束之前刪除默認約束。

對我來說,這個問題的主要原因是,我忘了取消AutoLayoutXib編輯器。 其實我在代碼中對XIB做了很多調整。

 for(UIView *view in [self.view subviews]) {
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
}

這幫助我抓住了導致問題的觀點。

上述答案都對我的情況沒有幫助。 我正在運行 XCode 10.1 並在“iPad(第 5 代)”模擬器上測試我的應用程序。 模擬器運行的是 iOS 12.1。

我的故事板中有一個簡單的根視圖,有兩個 UITextField 子視圖。 故事板中根本沒有使用任何約束。 我在應用程序或故事板中沒有 UIButtonBarView 對象。

當應用程序啟動並布置根視圖時,不會打印任何消息。 模擬設備旋轉時無。

但是在模擬器中,當我單擊其中一個文本字段時,鍵盤擴展從屏幕底部出現,盡管不是全鍵盤,它似乎從未出現在模擬器中。 但是在終端上打印出以下內容:

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:0x6000034e7700 h=--& v=--& UIKeyboardAssistantBar:0x7f9c7d714af0.height == 0   (active)>",
"<NSLayoutConstraint:0x6000034aba20 V:|-(0)-[_UIUCBKBSelectionBackground:0x7f9c7d51ec70]   (active, names: '|':_UIButtonBarButton:0x7f9c7d51de40 )>",
"<NSLayoutConstraint:0x6000034aba70 _UIUCBKBSelectionBackground:0x7f9c7d51ec70.bottom == _UIButtonBarButton:0x7f9c7d51de40.bottom   (active)>",
"<NSLayoutConstraint:0x6000034fb3e0 V:|-(0)-[_UIButtonBarStackView:0x7f9c7d715880]   (active, names: '|':UIKeyboardAssistantBar:0x7f9c7d714af0 )>",
"<NSLayoutConstraint:0x6000034fb750 V:[_UIButtonBarStackView:0x7f9c7d715880]-(0)-|   (active, names: '|':UIKeyboardAssistantBar:0x7f9c7d714af0 )>",
"<NSLayoutConstraint:0x6000034abc00 'UIButtonBar.maximumAlignmentSize' _UIButtonBarButton:0x7f9c7d51de40.height == UILayoutGuide:0x600002ef4e00'UIViewLayoutMarginsGuide'.height   (active)>",
"<NSLayoutConstraint:0x6000034d7cf0 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x600002ef4e00'UIViewLayoutMarginsGuide']-(9)-|   (active, names: '|':_UIButtonBarStackView:0x7f9c7d715880 )>",
"<NSLayoutConstraint:0x6000034d7c50 'UIView-topMargin-guide-constraint' V:|-(10)-[UILayoutGuide:0x600002ef4e00'UIViewLayoutMarginsGuide']   (active, names: '|':_UIButtonBarStackView:0x7f9c7d715880 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000034aba70 _UIUCBKBSelectionBackground:0x7f9c7d51ec70.bottom == _UIButtonBarButton:0x7f9c7d51de40.bottom   (active)>

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

在我看來,這一切都與我的應用程序無關,而與 Apple 如何創建自己的鍵盤視圖有關,即使我的小擴展程序已宣布與它結合。

所以問題仍然存在,我作為應用程序開發人員是否有責任做一些事情(假設這是一堆值得關注的事情)還是只是 Apple 自己的問題/錯誤?

FWIW,模擬更新的 iPad 型號時不會出現此約束問題消息,例如 iPad Pro 12.9 英寸(第 3 代)。 但是在模擬 9.7 英寸 iPad Pro 時確實會顯示該消息。所有人都聲稱他們運行的是 iOS 12.1。

我遇到了同樣的錯誤,但僅在特定視圖上,當我觸摸第一個文本字段,然后向下觸摸下一個文本字段時。

我正在為 iOS 13.4 編寫 SwiftUI

 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. 
    (
        "<NSLayoutConstraint:0x2809b6760 'assistantHeight' TUISystemInputAssistantView:0x105710da0.height == 44   (active)>",
        "<NSLayoutConstraint:0x2809ccff0 'assistantView.bottom' TUISystemInputAssistantView:0x105710da0.bottom == _UIKBCompatInputView:0x10525ae10.top   (active)>",
        "<NSLayoutConstraint:0x2809cccd0 'assistantView.top' V:|-(0)-[TUISystemInputAssistantView:0x105710da0]   (active, names: '|':UIInputSetHostView:0x105215010 )>",
        "<NSLayoutConstraint:0x2809ca300 'inputView.top' V:|-(0)-[_UIKBCompatInputView:0x10525ae10]   (active, names: '|':UIInputSetHostView:0x105215010 )>"
    )

    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x2809ccff0 'assistantView.bottom' TUISystemInputAssistantView:0x105710da0.bottom == _UIKBCompatInputView:0x10525ae10.top   (active)>

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

需要注意的一件事(至少這讓我絆倒了)是我正在從錯誤的視圖中刪除約束。 我試圖刪除的約束不是我視圖的子約束,所以當我這樣做時

myView.removeConstraint(theConstraint)

它實際上並沒有刪除任何東西,因為我需要打電話

myView.superView.removeConstraint(theConstraint)

因為約束在技術上是我認為的兄弟約束。

對於xib中的viewCircle,我也遇到了在日志中破壞約束的相同問題。 我幾乎嘗試了上面列出的所有內容,但沒有任何效果對我有用。 然后我嘗試更改在日志中破壞的高度約束的優先級(通過在 xib 上添加約束的標識符來確認)在此處輸入圖像描述

基本上,您只需要從關聯的視圖中刪除該約束。 例如,如果高度約束發出警告,只需將其從您的視圖中刪除; 它不會影響視圖。

迅捷 4

我只是在 viewDidLoad 中添加這一行並與我一起工作。

view.removeConstraints(view.constraints)

暫無
暫無

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

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