如何在运行时更改约束优先级 [英] How can I change constraints priority in run time
问题描述
我有一个具有动态高度的视图,我试图在运行时更改此视图高度优先级。
I have a view which has dynamic height and I am trying to change this view height priority in run time.
这是我的代码部分;
if (index == 0) {
surveyViewHeightConstraint.constant = 0;
surveyViewHeightConstraint.priority = 1000;
} else if (index == 1) {
surveyViewHeightConstraint.constant = 163;
surveyViewHeightConstraint.priority = 500;
}
我正在通过按钮操作更改索引。当我运行此代码时,我收到此错误:
I am changing index with a button action. When I run this code, I am getting this error:
*** Assertion failure in -[NSLayoutConstraint setPriority:], /SourceCache/Foundation/Foundation-1141.1/Layout.subproj/NSLayoutConstraint.m:174
我的错误是什么在这里?
What is my mistake in here?
推荐答案
优先级可能不会从非必需更改为必需,或从必需更改为非需要。如果OS X中的
NSLayoutPriorityRequired
或iOS中的UILayoutPriorityRequired
的优先级更改为较低优先级,则会引发异常,或者在将约束添加到视图后将较低优先级更改为所需的优先级。即使在视图上安装了约束,也允许从一个可选优先级更改为另一个可选优先级。
Priorities may not change from nonrequired to required, or from required to nonrequired. An exception will be thrown if a priority of
NSLayoutPriorityRequired
in OS X orUILayoutPriorityRequired
in iOS is changed to a lower priority, or if a lower priority is changed to a required priority after the constraints is added to a view. Changing from one optional priority to another optional priority is allowed even after the constraint is installed on a view.
使用优先级999而不是1000从技术上讲,这不是绝对必要的,但它将比其他任何东西都更优先。
Use priority 999 instead of 1000. It won't be absolutely required technically speaking, but it'll be a higher priority than anything else.
这篇关于如何在运行时更改约束优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!