xcode 无法同时满足约束 - Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26460327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:56:08  来源:igfitidea点击:

Unable to simultaneously satisfy constraints - Swift

iosxcodeswift

提问by alex

Summary:I just got finished doing auto-layout and when I run it, it is all scrunched together. Then I look at the command prompt and it says the error above. I've tried googling it but it just shows solutions for Obj-C and I'm doing it in Swift. How would I be able to solve this auto-layout issue?

总结:我刚刚完成了自动布局,当我运行它时,它全部被压缩在一起。然后我查看命令提示符,上面写着错误。我试过用谷歌搜索它,但它只显示了 Obj-C 的解决方案,我正在 Swift 中进行。我怎样才能解决这个自动布局问题?

Error:

错误:

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.

An Image of My Constraintshttps://www.dropbox.com/s/1l3ys9k7bdr8qrk/Screenshot%202014-10-20%2021.08.47.png?dl=0An image of my iPhone 4 Previewhttps://www.dropbox.com/s/7uoulzppalex2r8/Screenshot%202014-10-20%2021.09.48.png?dl=0Image of the iPhone 4 in the Simulatorhttps://www.dropbox.com/s/c1qedgoytcnetri/iOS%20Simulator%20Screen%20Shot%20Oct%2020%2C%202014%2C%209.10.03%20PM.png?dl=0

我的约束图片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

If you guys have questions or need clarification please comment down below

如果你们有问题或需要澄清,请在下面评论

回答by akshaynhegde

It would be a bit hard to track which is the bad constraint by just looking at the error you have posted. But I can give a general rules or things to keep in mind while working with autolayout or constraints in general,

仅通过查看您发布的错误来跟踪哪个是不好的约束会有点困难。但是我可以在使用自动布局或一般约束时给出一般规则或要记住的事情,

  • Prefer setting constraints in XIB/Storyboards. It will tell where exactly are you going wrong right when you are setting them. You can almost do everything in the interface builder unless its a special case.
  • Always set constraints in relative to views around it try not fix everything to super View
  • Use aspect ratio constraints whenever possible.
  • Try to use Intrinsic constraint sizes of views and avoid setting fixed widths/heights.
  • Remember you will always get into the kind of errors you are facing, when you set too many constraints. So always think are the constraints "just enough" to figure out its frame in run time. Not more or less but just enough.
  • Make use of in-equality constraints, they are very helpful. Also don't forget you have hugging Priorities and compression resistance priorities.
  • 更喜欢在 XIB/故事板中设置约束。当您设置它们时,它会告诉您究竟哪里出错了。除非是特殊情况,否则您几乎可以在界面构建器中完成所有操作。
  • 始终相对于它周围的视图设置约束尽量不要将所有内容都修复到超级视图
  • 尽可能使用纵横比约束。
  • 尝试使用视图的内在约束大小并避免设置固定的宽度/高度。
  • 请记住,当您设置太多约束时,您总会遇到所面临的错误。所以总是认为约束“刚好”可以在运行时找出它的框架。不多也不少,但刚好够用。
  • 利用不等式约束,它们非常有帮助。也不要忘记你有拥抱优先级和抗压优先级。

In your error, I see a lot of constraints which have fixed sizes, they would interfere when you have spacing constraint to those views. So start for the first, and keep the points I mentioned and delete the constraints you don't need.

在您的错误中,我看到许多具有固定大小的约束,当您对这些视图有间距约束时,它们会干扰。所以从第一个开始,保留我提到的要点并删除您不需要的约束。

References,

参考,

回答by Ronaldoh1

I had a hard time figuring out what constraints were causing this error. Here is a simpler way to do it.

我很难弄清楚是什么约束导致了这个错误。这是一个更简单的方法。

I'm using Xcode 6.1.1

我正在使用 Xcode 6.1.1

"Command + A" to select all the UILabels, UIImages etc. Click Editor -> Pin > (Select...) to Superview again click Editor -> Resolve Auto Layout Issues -> Add Missing Constraints or Reset to Suggested Constraints. It depends on your case.

“Command + A”选择所有 UILabels、UIImages 等。点击 Editor -> Pin > (Select...) to Superview 再次点击 Editor -> Resolve Auto Layout Issues -> Add Missing Constraints or Reset to Suggested Constraints。这取决于你的情况。