ios - 如何在运行时更改约束优先级

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

这是我的部分代码;

if (index == 0) {

    surveyViewHeightConstraint.constant = 0;
    surveyViewHeightConstraint.priority = 1000;

} else if (index == 1) {

    surveyViewHeightConstraint.constant = 163;
    surveyViewHeightConstraint.priority = 500;

}

我正在通过按钮操作更改索引。当我运行此代码时,我收到此错误:

*** Assertion failure in -[NSLayoutConstraint setPriority:], /SourceCache/Foundation/Foundation-1141.1/Layout.subproj/NSLayoutConstraint.m:174

我的错误是什么?

最佳答案

NSLayoutConstraint class reference 中所述:

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。从技术上讲,这不是绝对必要的,但它的优先级比其他任何东西都高。

https://stackoverflow.com/questions/31186187/

相关文章:

objective-c - 将参数传递给由 NSTimer 调用的方法

objective-c - 具有多个目标的 Objective C 到 Swift 头文件

objective-c - 将自定义 subview (在 xib 中创建)添加到 View Con

objective-c - 将 Xcode 组创建为文件系统文件夹的工作流程

ios - 如何使用图像和标签制作自定义 UIBarButtonItem?

ios - '无效更新 : invalid number of rows in section 0

ios - MKMapView:自定义 View 代替 Annotation Pin

ios - 按字典中键的值对字典的 NSArray 进行排序

ios - 如何检查 NSArray 中的对象是否为 NSNull?

iphone - 快速将单个引脚添加到 MKMapView?