【问题标题】:Confused by conflicting autolayout constraints对冲突的自动布局约束感到困惑
【发布时间】:2014-01-01 06:47:47
【问题描述】:

当我点击 tabbar tabitem 来改变 viewcontroller 时,我得到了这个崩溃日志:

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) 
(
"<NSAutoresizingMaskLayoutConstraint:0xdb98e30 h=--- v=--- H:[UIWindow:0xc65c530(320)]>",
"<NSAutoresizingMaskLayoutConstraint:0xdbba9a0 h=-&- v=-&- UITransitionView:0xdb43ab0.width == UILayoutContainerView:0xdb42ef0.width>",
"<NSAutoresizingMaskLayoutConstraint:0xc680b80 h=-&- v=-&- UILayoutContainerView:0xdb42ef0.width == UIWindow:0xc65c530.width>",
"<NSAutoresizingMaskLayoutConstraint:0x119352f0 h=-&- v=-&- UIViewControllerWrapperView:0xc68a2c0.width == UITransitionView:0xdb43ab0.width>"
)

 Will attempt to recover by breaking constraint 
<NSAutoresizingMaskLayoutConstraint:0x119352f0 h=-&- v=-&- UIViewControllerWrapperView:0xc68a2c0.width == UITransitionView:0xdb43ab0.width>

 Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

【问题讨论】:

  • 请更好地解释 - 你不明白什么。这是自动布局约束冲突问题,但我们无法帮助您了解您不了解的内容。
  • 我不明白为什么我会发生这种崩溃...这种崩溃并非每次都发生...sme times 应用程序因此而崩溃...如何解决这个问题?跨度>

标签: iphone ios7 autolayout autoresizingmask


【解决方案1】:

首先,您如何应用这些约束?这些冲突消息的最常见原因是添加约束而没有确保它们是视图唯一的约束。有没有设置属性

translatesAutoresizingMaskIntoConstraints = 否

在尝试设置更多之前?因为您在运行时遇到此异常,所以 Xcode 似乎已经接受了您正在尝试执行的操作,因此您可能正在代码中添加约束。

您是否在列表中添加了所有冲突的约束?如果是这样,请将它们删除并一一更换,以找出您错了。

如果您在 Xcode 中使用我们过去称为 Interface Builder 的方式执行所有这些操作,请使用新工具检查所有约束并验证您没有添加冲突的约束。 Xcode 可以在这里为您提供帮助。

如果您在代码中添加约束,但您已经在 IB 中创建了视图,那么您可能想要管理 IB 中的所有约束,或者您想要将 xib 设置为不使用自动布局,这样它就不会应用冲突的约束与您稍后添加的内容。

【讨论】:

  • 所以这意味着......在添加任何内容以查看标签或子视图或表格视图之前,我需要设置 translatesAutoresizingMaskIntoConstraints = NO。在代码中添加约束是什么意思?我没有指定任何约束。我只是通过代码添加标签或视图。如果通过代码创建标签正在添加约束,那么是的,我正在做与您在上一段中提到的相同的事情。我在 IB 中创建视图,然后通过代码添加组件。
  • 某处您开启了“使用自动布局”。如果您没有使用 addConstraint: 或 addConstraints: 那么您没有在代码中执行此操作(您会知道是否使用过)。那么您是否尝试过在界面生成器中解决约束?
  • 在使用自定义展示控制器以模态方式展示视图控制器时,我遇到了这些完全相同的警告。我的视图控制器是从 storyboard 实例化的; Apple 的文档似乎表明只有以编程方式创建的视图(即init(frame:))将此标志设置为true。显然,这只适用于您在 Interface Builder 中添加的 subviews,并且视图控制器的 main view alsotranslatesAutoresizingMaskIntoConstraints 设置为 @987654324 @ 默认情况下,即使从情节提要中实例化也是如此。
猜你喜欢
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
  • 2017-03-22
  • 2014-05-05
  • 2021-08-27
  • 2020-11-21
  • 2018-10-13
  • 2020-08-14
相关资源
最近更新 更多