iOS将视图添加为子视图,以编程方式自动布局冲突约束

时间:2014-10-31 15:12:58

标签: ios autolayout subview nslayoutconstraint

我有以下设置:

故事板中定义的2个视图控制器。

我有一个自定义segue,它通过激活第一个vc上的控件来触发。

在segue的-(void)perform方法中,我必须将第二个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)]>"
)

我相信这里发生了什么 - myView在构建时从其自己的vc中应用了约束,当我将其作为子视图添加到另一个视图时 - 事情变得混乱而且无法理解哪个约束适用 - 先前应用的那个 - 或者我已定义的那些。

我想知道解决这个问题的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

因此,为了实现这一目标,view正在采取行动。来自自己的viewController必须将其setTranslatesAutoresizingMaskIntoConstraints设置为NO

[myView setTranslatesAutoresizingMaskIntoConstraints:NO];