【发布时间】:2018-06-11 10:19:09
【问题描述】:
我有一个视图,我需要切换哪两个约束。
我添加了以下约束
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);
}];
}
现在当我尝试调整它的大小时,警告来了
【问题讨论】:
-
我认为你需要在添加后而不是在添加 deactivateConstraints 和 .active = NO 之前添加非活动约束,两者都是相同的
-
不,这不是问题
-
您可以在约束中添加标识符。稍后您可以通过过滤标识符上的约束来获得此约束,然后切换此约束的活动状态。
-
在 swift chatHalfLeadingConstraint.priority = UILayoutPriority.defaultLow 中让你的 chatHalfLeadingConstraint 优先级低
-
警告显示您同时激活了
chatHalfLeadingConstraint和chatFullLeadingConstraint,并且它们之间存在冲突。在任何给定时间,您应该只有其中一个处于活动状态。只有一个约束并根据需要更改constant会更简单;您甚至可以为这种变化制作动画以获得良好的过渡效果。
标签: ios objective-c autolayout