无法同时满足约束条件 - 斯威夫特

问题描述:

摘要: 我刚刚做完自动布局,当我运行它,它是所有缩紧在一起。然后我看看命令提示符,它说上面的错误。我试着用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

这将是一个有点难以跟踪哪些是坏的约束通过看您发布的错误。但我可以在处理自动布局或约束条件时记住一般规则或事项,

  • 优先考虑XIB/Storyboard中的设置约束。它会告诉你在设置它们时到底发生了什么。除非特殊情况,否则几乎可以在界面构建器中做所有事情。
  • 总是在相对于它周围的视图中设置约束条件尽量不要将所有东西都修复为超级查看
  • 尽可能使用宽高比约束。
  • 尝试使用视图的固有约束大小并避免设置固定的宽度/高度。
  • 记住,当你设置过多的约束条件时,你总是会遇到你所面临的那种错误。因此,总是认为这些约束“足够”,以便在运行时找出其框架。不多或少,但恰到好处。
  • 利用不平等约束,它们非常有帮助。另外不要忘记你拥有优先权和抗压力优先权。

在你的错误中,我看到很多具有固定大小的约束,当你对这些视图有间隔约束时,它们会产生干扰。因此,从第一个开始,保留我提到的要点并删除不需要的约束条件。

参考,

我做了一个艰难的时间来掌握OU什么约束导致了这个错误。这是一个更简单的方法。

我使用的Xcode 6.1.1

“命令+ A” 选中所有UILabels,UIImages等 点击编辑 - >针>(选择...),以上海华 再次点击编辑 - >解决自动布局问题 - >添加缺少约束或重置为建议的约束。这取决于你的情况。