簡體   English   中英

無法同時滿足約束 - Swift

[英]Unable to simultaneously satisfy constraints - Swift

總結:我剛剛完成了自動布局,當我運行它時,它全部被壓縮在一起。 然后我查看命令提示符,上面寫着錯誤。 我試過用谷歌搜索它,但它只顯示了 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 預覽圖片https://www.dropbox .com/s/7uoulzppalex2r8/Screenshot%202014-10-20%2021.09.48.png?dl=0模擬器中 iPhone 4 的圖像https://www.dropbox.com/s/c1qedgoytcnetri/iOS%20Simulator% 20Screen%20Shot%20Oct%2020%2C%202014%2C%209.10.03%20PM.png?dl=0

如果你們有問題或需要澄清,請在下面評論

僅通過查看您發布的錯誤來跟蹤哪個是不好的約束會有點困難。 但是在使用自動布局或一般約束時,我可以給出一般規則或要記住的事情,

  • 更喜歡在 XIB/故事板中設置約束。 當您設置它們時,它會告訴您究竟哪里出錯了。 除非是特殊情況,否則您幾乎可以在界面構建器中完成所有操作。
  • 始終相對於它周圍的視圖設置約束盡量不要將所有內容都修復到超級視圖
  • 盡可能使用縱橫比約束。
  • 嘗試使用視圖的內在約束大小並避免設置固定的寬度/高度。
  • 請記住,當您設置太多約束時,您總會遇到所面臨的錯誤。 所以總是認為約束“剛好”可以在運行時找出它的框架。 不多也不少,但剛好夠用。
  • 利用不等式約束,它們非常有用。 也不要忘記你有擁抱優先級和抗壓優先級。

在您的錯誤中,我看到許多具有固定大小的約束,當您對這些視圖有間距約束時,它們會干擾。 所以從第一個開始,保留我提到的要點並刪除您不需要的約束。

參考,

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

我正在使用 Xcode 6.1.1

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

暫無
暫無

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

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