gpt4 book ai didi

ios - 以编程方式添加 Controller 的 View 时自定义自动布局约束被破坏

转载 作者:可可西里 更新时间:2023-11-01 06:24:33 24 4
gpt4 key购买 nike

我有一个 myController,我以这种方式插入代码(在 self = Root View Controller 中):

    MyViewController* myController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
[self addChildViewController:myController];
[self.view addSubview:myController.view];
myController.view.frame = CGRectMake(0, 504, 320, 216);

在 MyViewController 的 xib 文件中,我只有 2 个 View ,一个在另一个之上。假设顶部 View 称为 View1,底部 View 称为 View2。 View0 是主视图。

我添加了以下垂直 NSLayoutConstraints : 五:[ View 2(40)] V:|-(0)-View1(名称:'|':View0) V:View2-(0)-| (名称:“|”:View0) V:View1-(0)-View2

也就是说 View1 在顶部接触 View0,在底部接触 View2。 View2 在顶部接触 View1,在底部接触 View0,高度恒定为 40。

当我以纵向方式运行时,一切似乎都正常。但是当我旋转到横向时,有时会出现以下错误。

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:0xd17cc00 h=--& v=--& V:[UIView:0xd174a30(268)]>",
"<NSLayoutConstraint:0x9469890 V:[View2(40)]>",
"<NSLayoutConstraint:0x9468e90 V:|-(0)-View1 (Names: '|':View0 )>",
"<NSLayoutConstraint:0x9468ef0 V:View2-(0)-| (Names: '|':View0 )>",
"<NSLayoutConstraint:0x94676f0 V:View1-(0)-View2>",
"<NSAutoresizingMaskLayoutConstraint:0x9450640 h=-&- v=-&- UIView:0000.height == UIView:0xd174a30.height - 268>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x9469890 V:[View2(40)]>

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.

有人明白我为什么会这样吗?我应该怎样做才能保持我之前定义的约束?

最佳答案

最后,为了解决这个问题,我不得不在 View0 上将 translatesAutoresizingMaskIntoConstraints 设置为 NO。

然后,为了很好地显示我的 View0,我必须为 View0 手动创建 NSLayoutConstraint,并将这些约束添加到 View0 的父 View 。

关于ios - 以编程方式添加 Controller 的 View 时自定义自动布局约束被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24391826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com