简体   繁体   中英

iOS add view as subview programatically autolayout conflicting constraints

I have the following setup:

2 view controllers defined in storyboard.

I have a custom segue that gets triggered by activating a control on the first vc.

in the -(void)perform method of the segue, I have to add the second vc's view as subview to the first with some animation.

-(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]];


}

Unfortunately I get the following error.

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)]>"
)

What I believe is happening here - myView has its constraints applied at build time 'from its own vc`, and when I am to add it as a subview to another view - things get messy and it cannot understand which constrains to apply - the ones applied earlier - or the ones I have defined.

I wonder what is the right way to solve this ?

因此,为了使这项工作的view正在被从自己的“取” viewController必须有其setTranslatesAutoresizingMaskIntoConstraints设置为NO

[myView setTranslatesAutoresizingMaskIntoConstraints:NO];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM