我怎样才能正确地进行非主动/主动约束?
Posted
技术标签:
【中文标题】我怎样才能正确地进行非主动/主动约束?【英文标题】:How can i do properly deactive/active constraint? 【发布时间】: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
会更简单;您甚至可以为这种变化制作动画以获得良好的过渡效果。
【参考方案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);
];
【讨论】:
另外,查看NSLayoutAnchor
- 这是一种比直接使用NSLayoutConstraint
更好的创建约束的方法
这仍然是问题我在第一次执行此行时收到警告消息 chatLeadingConstraint.constant = full ? 0:360 之后一切正常
警告中的冲突约束是什么?
(1) 查看每个约束并尝试找出您不期望的; (2) 找到添加了一个或多个不需要的约束的代码并修复它。 ( "<0x14ed0e20 h:><0x14dea580 h: :0x14eca060 names:><0x14ed0e20 h: names:>0x14ed0e20>0x14dea580>0x14ed0e20>
以上是关于我怎样才能正确地进行非主动/主动约束?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 htmx 在 django 中进行不区分大小写的主动搜索
UITableViewHeaderFooterView的使用+自己主动布局
如何使用 UIImageView 和单元格中的多个标签正确地进行约束?