如何在运行时更改约束优先级 [英] How can I change constraints priority in run time

查看:552
本文介绍了如何在运行时更改约束优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有动态高度的视图,我试图在运行时更改此视图高度优先级。

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?

推荐答案

NSLayoutConstraint 类引用


优先级可能不会从非必需更改为必需,或从必需更改为非需要。如果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 or UILayoutPriorityRequired 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆