2014-09-02 91 views
29

剛剛開始學習iOS的自動版式,界面生成非常簡單的,但是當我試圖存檔同一件事上的代碼自動版式,無法同時滿足的約束

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(==2)-[_nextKeyboardButton]-(==2)-[_numPadButton]-(==2)-[_spaceButton]-(==2)-[_returnButton]-(==2)-|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(_nextKeyboardButton,_numPadButton,_spaceButton,_returnButton)]]; 

它拋出一個異常,

無法同時滿足約束條件。

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) 
(
"<NSLayoutConstraint:0x6000000966c0 H:|-(2)-[UIButton:0x7fe4f1d1c760'Next'] (Names: '|':UIInputView:0x7fe4f1f04d00)>", 
"<NSLayoutConstraint:0x600000096710 H:[UIButton:0x7fe4f1d1c760'Next']-(2)-[UIButton:0x7fe4f1d1d1d0'123']>", 
"<NSLayoutConstraint:0x600000096760 H:[UIButton:0x7fe4f1d1d1d0'123']-(2)-[UIButton:0x7fe4f1d1d6f0'Space']>", 
"<NSLayoutConstraint:0x6000000967b0 H:[UIButton:0x7fe4f1d1d6f0'Space']-(2)-[UIButton:0x7fe4f1d1d8d0'Return']>", 
"<NSLayoutConstraint:0x600000096800 H:[UIButton:0x7fe4f1d1d8d0'Return']-(2)-| (Names: '|':UIInputView:0x7fe4f1f04d00)>", 
"<NSLayoutConstraint:0x600000096e40 'UIView-Encapsulated-Layout-Width' H:[UIInputView:0x7fe4f1f04d00(0)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000967b0 H:[UIButton:0x7fe4f1d1d6f0'Space']-(2)-[UIButton:0x7fe4f1d1d8d0'Return']> 

所有4個按鈕.translatesAutoresizingMaskIntoConstraints = NO;

我想知道什麼是錯的?幫助是非常感謝:)

FYI:我在iOS8上的SDK

+1

似乎是一個完全合乎邏輯的約束條件。在過去的幾天裏,我一直在抨擊我的頭腦。順便說一句,@Chamira Fernando在按鈕上還有其他寬度限制嗎?內部內容大小等?您是否嘗試將compressionResistancePriority或contentHuggingPriority設置爲高或低值?只要看看會發生什麼? – 2014-09-18 11:35:50

+3

約束失敗的原因顯然是由'UIView-Encapsulated-Layout-Width'的寬度爲0.'UIInputView'位於何處? – 2014-09-18 12:17:24

+0

傻我 - 我沒有看到(0)。我遇到過同樣的問題。它是由UIInputViewController提供的UIInputView - 鍵盤擴展的根視圖(iOS8)。我不知道爲什麼它會希望寬度爲0,考慮到默認行爲(根據文檔)應該堅持屏幕寬度。 – 2014-09-18 22:05:42

回答

2

工作,根據你的問題就我的理解,你已經創建了筆尖控制和您直接試圖改變是從你的類的限制。

你可以在這裏做的是在視圖控制器的nib中設置約束,然後將它與你的類綁定並使用約束對象。或者你可以編程創建視圖並直接設置約束。

//如果「是 - 視圖的超級視圖查看視圖的自動調整屏蔽,產生實現它的約束並將這些約束添加到自身(超級視圖),您還需要檢查是否需要」translatesAutoresizingMaskIntoConstraints「。 」如果‘No - 視圖的上海華盈不看視圖的自動尺寸面具,而不是產生了實現它的約束’

我也得到了同樣的問題,我修好瞭如下

 [_nextKeyboardButton setTranslatesAutoresizingMaskIntoConstraints:YES]; 
    _numPadButton.translatesAutoresizingMaskIntoConstraints = YES; 
    [_numPadButton updateConstraints]; 
    [_nextKeyboardButton updateConstraints]; 

    NSArray *constraints2 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"H:|-57-[_nextKeyboardButton(96)]" 
          options:0 
          metrics:nil 
          views:NSDictionaryOfVariableBindings(_nextKeyboardButton)]; 

    [self.view addConstraints:constraints2]; 

    NSArray *constraints4 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|-123-[_nextKeyboardButton(30)]" 
          options:0 
          metrics:nil 
          views:NSDictionaryOfVariableBindings(_nextKeyboardButton)]; 

    [self.view addConstraints:constraints4]; 

    NSArray *constraints1 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"H:|-207-[_numPadButton(58)]" 
          options:0 
          metrics:nil 
          views:NSDictionaryOfVariableBindings(_numPadButton)]; 

    [self.view addConstraints:constraints1]; 

    NSArray *constraints3 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|-123-[_numPadButton(30)]" 
          options:0 
          metrics:nil 
          views:NSDictionaryOfVariableBindings(_numPadButton)]; 

    [self.view addConstraints:constraints3]; 

// Create Controls or view programmatically. 

UILabel *label = [UILabel new]; 
    label.text = @"This is a Label"; 
    label.font = [UIFont systemFontOfSize:18]; 
    label.backgroundColor = [UIColor lightGrayColor]; 
    label.translatesAutoresizingMaskIntoConstraints = NO; 
    [self.view addSubview:label]; 

    NSArray *constraints = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|-offsetTop-[label(100)]" 
          options:0 
          metrics:@{@"offsetTop": @100} 
          views:NSDictionaryOfVariableBindings(label)]; 
    [self.view addConstraints:constraints]; 

    UIView *spacer1 = [UIView new]; 
    spacer1.translatesAutoresizingMaskIntoConstraints = NO; 
    [spacer1 setBackgroundColor:[UIColor redColor]]; 
    [self.view addSubview:spacer1]; 

    UIView *spacer2 = [UIView new]; 
    spacer2.translatesAutoresizingMaskIntoConstraints = NO; 
    [spacer2 setBackgroundColor:[UIColor greenColor]]; 
    [self.view addSubview:spacer2]; 

    NSArray *constraints1 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|-offsetTop-[spacer1(100)]" 
          options:0 
          metrics:@{@"offsetTop": @100} 
          views:NSDictionaryOfVariableBindings(spacer1)]; 

    [self.view addConstraints:constraints1]; 

    NSArray *constraints2 = [NSLayoutConstraint 
          constraintsWithVisualFormat:@"V:|-offsetTop-[spacer2(100)]" 
          options:0 
          metrics:@{@"offsetTop": @100} 
          views:NSDictionaryOfVariableBindings(spacer2)]; 

    [self.view addConstraints:constraints2]; 

    [self.view addConstraints:[NSLayoutConstraint 
           constraintsWithVisualFormat:@"H:|[spacer1]-10-[label]-20-[spacer2(==spacer1)]|" 
           options:0 
           metrics:nil 
           views:NSDictionaryOfVariableBindings(label, spacer1, spacer2)]]; 

所以當使用AutoLayout時,你應該從不直接設置視圖的框架。約束被用來爲你做這件事。通常,如果你想在視圖中設置你自己的約束,你應該重寫你的UIViews的updateConstraints方法。確保頁面控制器的內容視圖允許調整其邊緣的大小,因爲它們的大小將適合頁面視圖的框架。你的約束和視圖設置將需要考慮到這一點,否則你將得到不可滿足的約束錯誤。

https://developer.apple.com/library/mac/documentation/userexperience/conceptual/AutolayoutPG/AutoLayoutConcepts/AutoLayoutConcepts.html#//apple_ref/doc/uid/TP40010853-CH14-SW1 你也可以參考上面的蘋果鏈接深入研究,如果你仍然面臨問題,我會盡我所能的水平來解決你的問題。

8

當我嘗試在基於故事板的視圖控制器中手動創建所有自動佈局約束時(在Swift中,使用Snappy - Swift的Masonry端口),我不得不努力解決類似問題。

由於某些原因,Xcode在構建時會在NIB上生成自己的默認自動佈局約束集。這就是爲什麼我不能添加任何手動約束,因爲它們與自動添加的約束相沖突。

我解決了這個方式如下:

  • 打開你處理故事板視圖控制器。

  • 選擇視圖控制器和選擇編輯>解決自動佈局問題> []中視圖控制器的所有視圖>從菜單中選擇添加缺少約束

enter image description here

(這將確保不會產生額外的構建時間限制,並且所有約束現在都可見)。

  • 從您的視圖控制器選擇所有的約束條件:
從右側窗格中的下列複選框

enter image description here

  • 檢查:佔位符 - 在構建時刪除:

enter image description here

現在您可以在代碼中手動添加所有自動佈局約束。

+1

這對我來說完全排除了問題。 – aframe 2015-01-26 11:12:53

+0

我遇到了類似的問題,我有七個UIButtons排成一行。在StoryBoard中一切都很好,但是當我構建它時,我得到了很多「無法同時滿足約束」的例外,但它在模擬器上看起來很好。我想知道我是否應該對他們做些什麼?如果我這樣做,我不能使用編輯器解決約束問題,因爲我的StoryBoard中沒有任何內容。 – Liumx31 2015-09-01 03:07:03

77

如何找到不可滿足的約束條件最簡單的方法:

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

enter image description here

  • NSLayoutConstraint創建簡單的擴展:

SWIFT

extension NSLayoutConstraint { 

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

Objective-C的

@interface NSLayoutConstraint (Description) 

@end 

@implementation NSLayoutConstraint (Description) 

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

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

enter image description here

  • ,一旦你有你的id你可以簡單的點擊它你查找導航

enter image description here

  • ,並迅速找到它:

enter image description here

如何簡單地修復這種情況?

  • 嘗試改變優先999斷開的約束。

看到相關答案在這裏:Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint

+9

一個令人難以置信的提示。 – Fattie 2015-09-23 12:17:34

+1

這是我讀過的用於調試約束問題的最有用的東西。把手放下。謝謝! – Xeaza 2015-12-03 02:01:16

+0

ty,這項工作不錯 – 2015-12-18 20:59:37

3

這可能是不那麼difficult.The消息控制檯輸出表示,UIButton的的限制redefined.It意味着只是喜歡你把限制Height和約束Width這個按鈕,但是不必要的是你再加上一個限制,即按鈕有一個Aspect Ratio。在這種情況下,Xcode不能確定遵循哪個約束,以便你可以在控制檯中看到這個調試消息。

有兩種方法有用的我:

  1. 分析顯示在控制檯的限制,並嘗試刪除一個或多個約束。

2。如果您使用的是storyboardXib,請選擇約束 - >顯示屬性檢查器 - >更改某些約束的優先級。


一件事...

您可以按照傑森賈勒特的article。添加一個符號斷點以確保哪個視圖使錯誤發生。

1

萬一別人運行到這一點,一個簡單的方法來直觀地檢查你的約束問題,是這個網站(它真的有用!):

http://www.wtfautolayout.com

下面是情況下,Github上項目現場下降: https://github.com/johnpatrickmorgan/wtfautolayout

+0

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效 – slfan 2017-09-04 20:45:07

相關問題