2014-10-20 72 views
0

摘要: 我剛剛做完自動佈局,當我運行它,它是所有縮緊在一起。然後我看看命令提示符,它說上面的錯誤。我試着用Google搜索它,但它只是顯示Obj-C的解決方案,而我正在Swift中完成它。我將如何解決這個自動佈局問題?無法同時滿足約束條件 - 斯威夫特

錯誤:

2014-10-20 00:07:41.102 Fraction Calculator[74247:6698760] 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) 
(
    "<NSLayoutConstraint:0x7c440f10 V:[UIButton:0x7c440db0'Divide It!'(30)]>", 
    "<NSLayoutConstraint:0x7af7bdb0 V:[UIImageView:0x7af7bae0(8)]>", 
    "<NSLayoutConstraint:0x7ae928b0 V:[UITextField:0x7ae952d0(30)]>", 
    "<NSLayoutConstraint:0x7ae97110 V:[UITextField:0x7ae96f70(30)]>", 
    "<NSLayoutConstraint:0x7ae97eb0 V:[UILabel:0x7ae97d70'Enter Your Fraction To Be...'(48)]>", 
    "<NSLayoutConstraint:0x7ae99cd0 V:[_UILayoutGuide:0x7ae990c0]-(0)-[UILabel:0x7ae97d70'Enter Your Fraction To Be...']>", 
    "<NSLayoutConstraint:0x7ae9a000 V:[UILabel:0x7ae97d70'Enter Your Fraction To Be...']-(41)-[UITextField:0x7ae96f70]>", 
    "<NSLayoutConstraint:0x7ae9a090 V:[UITextField:0x7ae96f70]-(14)-[UIImageView:0x7af7bae0]>", 
    "<NSLayoutConstraint:0x7ae9a150 V:[UIImageView:0x7af7bae0]-(13)-[UITextField:0x7ae952d0]>", 
    "<NSLayoutConstraint:0x7ae9a270 V:[UITextField:0x7ae952d0]-(59)-[UIButton:0x7c440db0'Divide It!']>", 
    "<NSLayoutConstraint:0x7ae9a2a0 V:[UIButton:0x7c440db0'Divide It!']-(50)-[UILabel:0x7ae98df0]>", 
    "<NSLayoutConstraint:0x7ae9a300 V:[UILabel:0x7ae98df0]-(166)-[_UILayoutGuide:0x7ae99680]>", 
    "<_UILayoutSupportConstraint:0x7c441e80 V:[_UILayoutGuide:0x7ae990c0(20)]>", 
    "<_UILayoutSupportConstraint:0x7c441770 V:|-(0)-[_UILayoutGuide:0x7ae990c0] (Names: '|':UIView:0x7af7abc0)>", 
    "<_UILayoutSupportConstraint:0x7c443610 V:[_UILayoutGuide:0x7ae99680(0)]>", 
    "<_UILayoutSupportConstraint:0x7c4429d0 _UILayoutGuide:0x7ae99680.bottom == UIView:0x7af7abc0.bottom>", 
    "<NSLayoutConstraint:0x7c44acf0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7af7abc0(480)]>" 
) 

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ae97eb0 V:[UILabel:0x7ae97d70'Enter Your Fraction To Be...'(48)]> 

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. 

我的約束 https://www.dropbox.com/s/1l3ys9k7bdr8qrk/Screenshot%202014-10-20%2021.08.47.png?dl=0 的iPhone 4我的iPhone 4預覽 https://www.dropbox.com/s/7uoulzppalex2r8/Screenshot%202014-10-20%2021.09.48.png?dl=0 圖像在模擬器的圖像的圖像 https://www.dropbox.com/s/c1qedgoytcnetri/iOS%20Simulator%20Screen%20Shot%20Oct%2020%2C%202014%2C%209.10.03%20PM.png?dl=0

如果你們有任何疑問或需要澄清請評論向下跌破

+2

如果您找到的解決方案位於Objective C中,它會產生什麼樣的差異?它是相同的API,所以你可以在Swift中簡單地重寫它們。 – sapi 2014-10-20 09:02:16

回答

4

這將是一個有點難以跟蹤哪些是壞的約束通過看您發佈的錯誤。但我可以在處理自動佈局或約束條件時記住一般規則或事項,

  • 優先考慮XIB/Storyboard中的設置約束。它會告訴你在設置它們時到底發生了什麼。除非特殊情況,否則幾乎可以在界面構建器中做所有事情。
  • 總是在相對於它周圍的視圖中設置約束條件儘量不要將所有東西都修復爲超級查看
  • 儘可能使用寬高比約束。
  • 嘗試使用視圖的固有約束大小並避免設置固定的寬度/高度。
  • 記住,當你設置過多的約束條件時,你總是會遇到你所面臨的那種錯誤。因此,總是認爲這些約束「足夠」,以便在運行時找出其框架。不多或少,但恰到好處。
  • 利用不平等約束,它們非常有幫助。另外不要忘記你擁有優先權和抗壓力優先權。

在你的錯誤中,我看到很多具有固定大小的約束,當你對這些視圖有間隔約束時,它們會產生干擾。因此,從第一個開始,保留我提到的要點並刪除不需要的約束條件。

參考,

2

我做了一個艱難的時間來掌握OU什麼約束導致了這個錯誤。這是一個更簡單的方法。

我使用的Xcode 6.1.1

「命令+ A」 選中所有UILabels,UIImages等 點擊編輯 - >針>(選擇...),以上海華 再次點擊編輯 - >解決自動佈局問題 - >添加缺少約束或重置爲建議的約束。這取決於你的情況。