简体   繁体   English

我可以忽略“无法同时满足约束”吗?

[英]Can I just ignore “Unable to simultaneously satisfy constraints”?

I'm updating an existing app to autolayout to support more device sizes. 我正在将现有应用更新为自动布局以支持更多设备尺寸。 I have a few objects which need to animate and instead of working with constrains I set them to: 我有一些需要动画的对象,而不是使用约束我将它们设置为:

self.secondCounter.translatesAutoresizingMaskIntoConstraints = YES //example

to use the old setFrame methods. 使用旧的setFrame方法。

This seems to work but throws an runtime error on first animation: 这似乎工作但在第一个动画上抛出运行时错误:

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; 试试这个:(1)看看每个约束并试着找出你不期望的东西; (2) find the code that added the unwanted constraint or constraints and fix it. (2)找到添加了不需要的约束或约束的代码并修复它。 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", "" ) (注意:如果你看到你不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性translatesAutoresizingMaskIntoConstraints的文档)(“”,“”,“”)

Will attempt to recover by breaking constraint 将尝试通过打破约束来恢复

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以便在调试器中捕获它。 The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 在列出的UIView上的UIConstraintBasedLayoutDebugging类别中的方法也可能有所帮助。 2014-10-07 19:41:12.903 AppName[5018:1153922] Unable to simultaneously satisfy constraints. 2014-10-07 19:41:12.903 AppName [5018:1153922]无法同时满足约束条件。 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; 试试这个:(1)看看每个约束并试着找出你不期望的东西; (2) find the code that added the unwanted constraint or constraints and fix it. (2)找到添加了不需要的约束或约束的代码并修复它。 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", "" ) (注意:如果你看到你不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性translatesAutoresizingMaskIntoConstraints的文档)(“”,“”,“”)

Will attempt to recover by breaking constraint 将尝试通过打破约束来恢复

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. 在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以便在调试器中捕获它。 The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 在列出的UIView上的UIConstraintBasedLayoutDebugging类别中的方法也可能有所帮助。

In fact I don't get this message because I don't set any constrains on this object (I heard Xcode sets them per default on build for every view in an auto layout xib). 事实上我没有得到这个消息,因为我没有在这个对象上设置任何约束(我听说Xcode在构建时为默认的自动布局xib中的每个视图设置它们)。

But somehow everything looks good and seems to work. 但不知何故,一切看起来都不错,似乎有效。

Can I just ignore this message or will I run into problems later? 我可以忽略此消息,还是稍后会遇到问题?

You will definitely run into problems later and need to resolve this. 肯定会遇到问题,需要解决这个问题。 If you don't satisfy all your constraints and the runtime correction system can't 'fudge the numbers', it can cause your app to crash. 如果您不满足所有约束并且运行时更正系统无法“捏造数字”,则可能导致您的应用程序崩溃。 You can get nasty messages like this one: 你可以得到像这样的令人讨厌的消息:

Fatal Exception: NSInvalidArgumentException. Unable to create description in descriptionForLayoutAttribute_layoutItem_coefficient. Something is nil

In order to solve autolayout issues, follow some simple steps: 要解决自动布局问题,请执行以下一些简单步骤:

  1. Find the view with the offending layouts. 找到具有违规布局的视图。 They will have a yellow or red icon by the View Controller's name in the left-side list of storyboard objects in the upper-right: 它们将在右上角的故事板对象左侧列表中通过视图控制器的名称显示黄色或红色图标:

黄色图标是你的向导......

  1. Resolve the issues by changing the frames, the constraints, or adding new constraints. 通过更改框架,约束或添加新约束来解决问题。 The issues themselves each come with an explanation, and you can find an excellent tutorial here: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/ResolvingIssues/ResolvingIssues.html 这些问题本身都有一个解释,你可以在这里找到一个很好的教程: https//developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/ResolvingIssues/ResolvingIssues.html
  2. When you have resolved all your issues, make sure you test! 当您解决了所有问题后,请确保进行测试! Test your view under the iPhone 4, 5, 6 and 6 Plus sizes in the simulator, and iPad if appropriate. 在模拟器和iPad(如果适用)下测试iPhone 4,5,6和6 Plus尺寸下的视图。 Also, be sure to test under iOS 7 and 8 - some autolayout conventions are fine in iOS 8 but crash iOS 7 apps. 另外,一定要在iOS 7和8下进行测试 - 在iOS 8中可以使用一些autolayout约定,但是iOS 7应用程序崩溃了。 (Learned that the hard way...) (学到了很难...)

That's it! 而已! Find, fix, test :) 查找,修复,测试:)

IMHO, The exception is a red herring. 恕我直言,例外是红鲱鱼。

The constraints are implicitely managed by apple. 这些限制是由苹果隐含的管理。 At some point a few lines of code 'over there' can indirectly affect them and thus it's a pain to discover why your code is raising an exception 'over here' 在某些时候,“那边”的几行代码可以间接地影响它们,因此发现为什么你的代码在这里引发异常是一件痛苦的事。

In my case i discovered a [cell setHidden:YES] went ahead and replaced that code with [cell removeFromSuperview] and the problem went away. 在我的情况下,我发现[cell setHidden:YES]继续并用[cell removeFromSuperview]替换了该代码,问题就消失了。

.... One of the tenants of OO programing is to make the APIS safe and easy to use. .... OO编程的租户之一是使APIS安全且易于使用。 Not easily bruised and confused. 不容易伤痕累累和困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM