OGeek|极客世界-中国程序员成长平台

标题: ios - 在容器 View 约束集合上使用 activateConstraints 时出错 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 15:03
标题: ios - 在容器 View 约束集合上使用 activateConstraints 时出错

在我使用尺寸类的 iOS 8 应用程序中,为了让 iPad 具有不同的纵向和横向布局,我为激活和停用的每个方向设置了一个 IBOutletCollection。在引入 Container View 之前,这一直很有效。

我正在添加一个新的 VC,它是 iPhone 版本中的一个单独的选项卡项,但我使用容器 View 将它嵌入到 iPad 布局中。我将只关注一个大小类,RegularRegular。容器 View 有 4 个约束:Trailing、Leading、Top 和 Bottom。当我运行应用程序时,它看起来很好(在那个单一方向上)。

现在,当我将这 4 个约束添加到我的 IBOutletCollection 时,activateConstraints 消息失败并显示以下错误消息:

*** 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法使用项目激活约束;层 = ;内容偏移:{0, 0}; contentSize: {460, 78}> 和 > 因为它们没有共同的祖先。约束是否引用不同 View 层次结构中的项目?这是非法的。'

约束不引用不同 View 层次结构中的项目。 Top、Bottom 和Leading 都与VC View 中的另一个 View 相关。 Trailing 与 VC 认为的步进器有关。因此,据我所知,它们都处于同一等级。

在调试时,我可以看到约束以 active 属性作为 nil 开始。这似乎是正常的(非容器约束在设置为 YES 之前开始相同)。我通过设置 active 属性的 for 循环检查了这一点。

我对容器 View 的约束有什么问题吗?容器 View 上的约束是否因为它们位于容器 View 上而有些特殊?我试过自己找到答案,但我似乎找不到关于我遇到的这个问题的任何东西。

任何帮助将不胜感激。

4/10 编辑以提供图片和代码。请注意,此时我已将容器约束分解为一个单独的数组,以便我可以自己处理它们。

在下图中,容器的Leading、Top 和Bottom 约束与容器右侧的 View 相关。尾随约束与其上方的步进器有关。 VC Layout

相关代码:

@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *regularAnyConstraints;
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *regularAnyContainerConstraints;

@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *anyRegularConstraints;
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *anyRegularContainerConstraints;

这些是 _anyRegularContainerConstraints 数组中的约束: _anyRegularContainerConstraints

[NSLayoutConstraint deactivateConstraints:_regularAnyConstraints];
[NSLayoutConstraint activateConstraints:_anyRegularConstraints];

[NSLayoutConstraint deactivateConstraints:_anyRegularContainerConstraints];
[NSLayoutConstraint activateConstraints:_anyRegularContainerConstraints];



Best Answer-推荐答案


When in debug, I can see that the constraint starts off with its active property as nil

这句话让我怀疑你是否误解了 activateConstraintsdeactivateConstraints 的作用。尽管他们的名字和文档中的误导性条目,他们实际上添加和删除了约束。因此,与其对这些命令感到困惑,我建议您最好直接调用 addConstraintsremoveConstraints。这样做的好处是:

我意识到这并不能直接回答您的问题,但您没有为此提供足够的信息(您没有显示调用 activateConstraintsdeactivateConstraints,您没有显示 View Controller 和 View 层次结构等的任何图表),因此尚不完全清楚您哪里出错了。相反,我试图让您在尝试解决问题的细节时重新调整您的思维

关于ios - 在容器 View 约束集合上使用 activateConstraints 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29552152/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4