我有一个视图需要切换两个约束。
我添加了以下约束
chatHalfLeadingConstraint = [NSLayoutConstraint
constraintWithItem:chatHistoryChildViewController.view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:360];
chatFullLeadingConstraint = [NSLayoutConstraint
constraintWithItem:chatHistoryChildViewController.view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.f];
NSLayoutConstraint *traling = [NSLayoutConstraint
constraintWithItem:chatHistoryChildViewController.view
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:0.f];
NSArray *chatvertConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[childView]|"
options:0
metrics:nil
views:@{@"childView" : chatHistoryChildViewController.view}];
chatFullLeadingConstraint.active = NO;
[self.view addConstraint:chatHalfLeadingConstraint];
[self.view addConstraint:chatFullLeadingConstraint];
[self.view addConstraint:traling];
[self.view addConstraints:chatvertConstraints];
[NSLayoutConstraint deactivateConstraints:@[chatFullLeadingConstraint]];
我需要停用chatFullLeadingConstraint。在UI中它看起来很好,但它在控制台中显示以下错误。
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.
(
"<NSLayoutConstraint:0x145f7c00 H:|-(360)-[UILayoutContainerView:0x145b8e50] (active, names: '|':UIView:0x1458a850 )>",
"<NSLayoutConstraint:0x145f7c50 H:|-(0)-[UILayoutContainerView:0x145b8e50] (active, names: '|':UIView:0x1458a850 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x145f7c00 H:|-(360)-[UILayoutContainerView:0x145b8e50] (active, names: '|':UIView:0x1458a850 )>
已编辑的代码
[self.view addConstraint:chatHalfLeadingConstraint];
//[self.view addConstraint:chatFullLeadingConstraint];
[self.view addConstraint:traling];
[self.view addConstraints:chatvertConstraints];
// chatFullLeadingConstraint.active = NO;
// [NSLayoutConstraint deactivateConstraints:@[chatFullLeadingConstraint]];
-(void)resizeRightPanelwithBlock:(OnResize)block;{
[UIView animateWithDuration:0.2 delay:0 options:0 animations:^{
if (isFullScreen == false) {
isFullScreen = true;
[self.view removeConstraint:chatHalfLeadingConstraint];
[self.view addConstraint:chatFullLeadingConstraint];
}else{
isFullScreen = false;
[self.view addConstraint:chatHalfLeadingConstraint];
[self.view removeConstraint:chatFullLeadingConstraint];
}
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
block(finished);
}];
}
现在,当我尝试调整大小时,警告来了
答案 0 :(得分:1)
不要删除和添加约束。添加一个约束。保持对它的引用。根据需要将其常量属性更改为0或360。
chatLeadingConstraint = [NSLayoutConstraint
constraintWithItem:chatHistoryChildViewController.view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:360];
NSLayoutConstraint *trailing = [NSLayoutConstraint
constraintWithItem:chatHistoryChildViewController.view
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:0.f];
NSArray *chatvertConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[childView]|"
options:0
metrics:nil
views:@{@"childView" : chatHistoryChildViewController.view}];
[self.view addConstraint:chatLeadingConstraint];
[self.view addConstraint:trailing];
[self.view addConstraints:chatvertConstraints];
-(void)resizeRightPanelToFull:(Bool)full completion:(OnResize)block;{
chatLeadingConstraint.constant = full ? 0:360
[UIView animateWithDuration:0.2 delay:0 options:[UIViewAnimationOptions.CurveEaseInOut] animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
block(finished);
}];
}