2016-08-19 68 views
1

好吧我有這個奇怪的問題(因爲我從來沒有見過像這樣的任何錯誤),我有一個Textfield在我看來,我擴大其動畫寬度雙方(拖尾和先行),但問題是這樣的它只是從一個側面擴展(或尾隨或領導)Xcode是不讓視圖保持水平居中

我的代碼:

視圖控制器類

@IBOutlet var constraint1: NSLayoutConstraint! // textfield's leading constraint 
@IBOutlet var constraint2: NSLayoutConstraint! // textfield's trailing constraint 

    viewDidload(){........} 

    @IBAction func editingBegin(sender: AnyObject) { 

    fullnameTextField.layer.cornerRadius = 0 
    fullnameTextField.clipsToBounds = false 
    self.constraint2.constant = -16 // here i'm expanding the width by setting constant of my Textfield 
    self.constraint1.constant = -16 
    UIView.animateWithDuration(duration) { 
     self.fullnameTextField.layoutIfNeeded() 
    } 
} 

我的NSLog錯誤:

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:0x155e77fb0 UITextField:0x155d85e40.trailing == UIView:0x155e46800.trailingMargin>", 
    "<NSLayoutConstraint:0x1546b02b0 UITextField:0x155d85e40.leading == UIView:0x155e46800.leadingMargin - 16>", 
    "<NSLayoutConstraint:0x1546447e0 UITextField:0x155d85e40.centerX == UIView:0x155e46800.centerX>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1546447e0 UITextField:0x155d85e40.centerX == UIView:0x155e46800.centerX> 

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. 

這裏是我的故事板在那裏我設置的限制

enter image description here

奇怪的是,我做的與其他瀏覽器同樣的事情,它在這裏工作有但不是,我檢查了他們是正確的每一個約束,並且該NSView錯誤也出現在該ViewController(上述工作正常)

任何人有任何線索最新錯誤在這裏?請讓我知道我的ViewController它如此令人沮喪

代碼http://pastebin.com/BvfEvzPE

+1

你的主要約束應該只有'16'而尾隨將是'-16'。 –

+0

@DipenPanchasara嘿男人你的答案訣竅 –

+1

謝謝你。快樂編碼:) –

回答

1

嘗試刪除的AlignX約束;因爲您已經有引導和拖尾約束,所以不需要AlignX。

您可以從這裏下載示例代碼並將其與您的相比較;

http://www.filedropper.com/tst

+0

感謝回覆的男人,一直在那裏做,沒有變化。 –

1

首先,作爲@Shoaib說,除去Center.x約束。 Center X + Width或Leading + Trailing就足夠了。

但即便如此,佈局引擎已經爲您打破了Center.x約束,並且都應該按原樣運行。但是,通過日誌消息判斷,只有「前導」約束已被修改(具有常數-16)。 「尾隨」似乎沒有被修改。 確保您的IBOutlet連接了兩個約束。

UPDATE:其實,outlet應該可以,因爲它是Swift,否則它會崩潰。我建議你一行一行通過你的代碼,看看什麼時候出現破壞約束日誌消息。

更新2(來自評論):你確定視圖的順序在約束中是正確的嗎?我的意思是,取決於哪個視圖是第一個視圖,哪個視圖是第二個視圖,您需要將常量設置爲+16或-16。嘗試使用+/-進行演奏,因爲它比轉到故事板並切換視圖順序要快。也許你應該爲約束指定+16。

+0

感謝您的迴應,我檢查了幾次連接是正確的,我也試圖通過消除centerX約束仍然沒有改變 –

+0

@remyboys,是的,對不起,我沒有把它想好。連接不可能是零,因爲它是Swift。否則會崩潰。查看更新。我會根據您的進一步反饋嘗試更新我的答案。 – FreeNickname

+0

請檢查我在這裏添加的整個代碼http://pastebin.com/BvfEvzPE,我自己檢查了它,但沒有發現任何問題 –

1

您可以嘗試更改CENTREX約束的優先級爲999

enter image description here

點擊編輯選項,你可以優先級改爲999

希望工程!