以编程方式添加控制器视图时,自定义自动布局约束会中断

 风暴工会 发布于 2023-01-07 19:05

我有一个myController,我在代码中插入(在self =一个根视图控制器中)这样:

    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个视图,一个在另一个上面.让我们说顶视图称为View1,底部视图称为View2.View0是主视图.

我添加了以下垂直NSLayoutConstraints:V:[View2(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) 
(
"",
"",
"",
"",
"",
""
)

Will attempt to recover by breaking constraint 


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

有人知道我为什么这么做吗?我应该做些什么来保持我定义的约束为先?

1 个回答
  • 好的,最后,为了解决这个问题,我不得不在View0上将translatesAutoresizingMaskIntoConstraints设置为NO.

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

    2023-01-07 19:06 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有