iOS以编程方式将视图添加为子视图自动布局冲突约束
我有以下设置: 故事板中定义了2个视图控制器 我有一个自定义的segue,它通过在第一个vc上激活一个控件来触发 在segue的iOS以编程方式将视图添加为子视图自动布局冲突约束,ios,autolayout,subview,nslayoutconstraint,Ios,Autolayout,Subview,Nslayoutconstraint,我有以下设置: 故事板中定义了2个视图控制器 我有一个自定义的segue,它通过在第一个vc上激活一个控件来触发 在segue的-(void)perform方法中,我必须将第二个vc视图作为子视图添加到第一个vc视图中,并添加一些动画 -(void)perform{ MyVC *myVC = self.destinationViewController; UIViewController *sourceVC = self.sourceViewController;
-(void)perform
方法中,我必须将第二个vc视图作为子视图添加到第一个vc视图中,并添加一些动画
-(void)perform{
MyVC *myVC = self.destinationViewController;
UIViewController *sourceVC = self.sourceViewController;
UIView *myView = myVC.view;
[sourceVC.view addSubview:myView];
NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(sourceVC.view, drawerView);
[myView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[myView(200)]"
options:0 metrics:nil views:viewDictionary]];
}
不幸的是,我得到了以下错误
2014-10-31 16:57:33.899 ReviewsAgain[19971:5435238] 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)
(
"<NSLayoutConstraint:0x7fb0306368c0 H:[UIView:0x7fb0306337e0(200)]>",
"<NSLayoutConstraint:0x7fb030635c80 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fb0306337e0(375)]>"
)
2014-10-31 16:57:33.899再次查看[19971:5435238]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
""
)
我相信这里正在发生的事情-myView
在构建时“从自己的vc”应用了它的约束,当我将它作为子视图添加到另一个视图时-事情变得混乱,它无法理解要应用哪些约束-之前应用的约束-或者我定义的约束
我想知道解决这一问题的正确方法是什么?因此,要使其工作,从自己的
视图控制器
中“获取”的视图必须将其设置为TranslatesAutoResistingGMaskintoConstraints
设置为否
[myView setTranslatesAutoresizingMaskIntoConstraints:NO];