Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在运行时更改约束优先级_Ios_Objective C_Autolayout_Constraints - Fatal编程技术网

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

Ios 如何在运行时更改约束优先级,ios,objective-c,autolayout,constraints,Ios,Objective C,Autolayout,Constraints,我有一个具有动态高度的视图,我正在尝试在运行时更改此视图高度优先级 这是我的部分代码 if (index == 0) { surveyViewHeightConstraint.constant = 0; surveyViewHeightConstraint.priority = 1000; } else if (index == 1) { surveyViewHeightConstraint.constant = 163; surveyViewHeightC

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

这是我的部分代码

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
我的错误是什么?

如中所述:

优先级不得从非必需更改为必需,或从必需更改为非必需。如果OS X中的
NSLayoutPriorityRequired
优先级或iOS中的
UILayoutPriorityRequired
更改为较低优先级,或者在将约束添加到视图后将较低优先级更改为所需优先级,将引发异常。即使在视图上安装了约束,也允许从一个可选优先级更改为另一个可选优先级


使用优先级999而不是1000。从技术上讲,它不是绝对必需的,但它的优先级比其他任何东西都高。

我们一直以来处理这一问题的方法是不更改约束常量,只更改优先级。例如,在您的情况下,有两个高度约束

 heightConstraintOne (who's height is set in the storyboard at 150, and priority set at 750)
 heightConstraintTwo (who's height is set in the storyboard at 0, and priority set at 250)
如果要隐藏视图,请执行以下操作:

heightConstraintTwo.priority = 999;
同样,如果要显示视图,请执行以下操作:

heightConstraintTwo.priority = 250;

我想对西里尔的回答补充一点

如果要在代码中创建约束,请确保在将其激活之前设置其优先级。例如:

surveyViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self
                                               attribute:NSLayoutAttributeHeight
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:self.superview
                                               attribute:NSLayoutAttributeHeight
                                              multiplier:1
                                                constant:0];
surveyViewHeightConstraint.active = YES;
surveyViewHeightConstraint.priority = 999;
这将导致运行时异常

不支持在已安装的约束上将优先级从required变为not(反之亦然)

正确的顺序是:

surveyViewHeightConstraint.priority = 999;
surveyViewHeightConstraint.active = YES;
对于Swift版本4+

constraintName.priority = UILayoutPriority(rawValue: 999)

根据
NSLayoutConstraints类
内部
UIKit模块

如果约束的优先级低于 UILayoutPriorityRequired,则它是可选的。更高优先级 在低优先级约束之前满足约束。 约束满足不是全部或全无。如果约束'a==b'是可选的,这意味着我们将尝试最小化 ‘abs(a-b)’。 此属性只能作为初始设置的一部分或在可选时进行修改。将约束添加到视图后,将显示 如果优先级从/更改为,将引发异常 NSLAYOUTPRIORITREQUIRED

示例:-
ui按钮
具有不同优先级的约束-

 func setConstraints() {
        buttonMessage.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint(item: buttonMessage, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: -10).isActive = true

        let leading = NSLayoutConstraint(item: buttonMessage, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 10)

        leading.isActive = true


        let widthConstraint = NSLayoutConstraint(item: buttonMessage, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100)

        let heightConstraint = NSLayoutConstraint(item: buttonMessage, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 50)


        let trailingToSuperView = NSLayoutConstraint(item: buttonMessage, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0)

        trailingToSuperView.priority = 999
        trailingToSuperView.isActive = true

        //leading.isActive = false//uncomment & check it will align to right of the View

        buttonMessage.addConstraints([widthConstraint,heightConstraint])

         }  
Swift版本:

myContraint.priority = UILayoutPriority(999.0)

现在,您可以将IBOutlet连接作为约束条件,然后使用此代码

self.webviewHeight.priority = UILayoutPriority(rawValue: 1000)

我希望这个场景对某人有所帮助: 我有两个约束条件,它们彼此相反,我的意思是它们不能同时满足,在interface builder中,一个优先级为1000,另一个优先级小于1000。当我在代码中更改它们时,出现了以下错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因是:不支持将已安装约束上的优先级从必需更改为非(反之亦然)。您通过了优先级250,而现有优先级为1000。”


然后我在viewDidLoad函数中用.defaultHigh和.defaultLow值初始化了它们,这就解决了我的问题。

我也面临同样的问题。正如上面在iOS 13中提到的一些答案一样,更改优先级可以正常工作,但在iOS 12中会导致崩溃

我可以通过在情节提要中创建ibnslayoutConstraint来解决这个问题,将优先级保持在1000,下面是修复代码

if (Condition) {
    surveyViewHeightConstraint.constant = 0;
    surveyViewHeightConstraint.isActive = false;
} else if (index == 1) {
    surveyViewHeightConstraint.constant = 163;
    surveyViewHeightConstraint.isActive = True;
}

希望这有帮助!!!干杯

谢谢你的回答,但是如果我使用999而不是1000,我无法通过将0指定给高度约束来隐藏我的视图。这是另一个问题。您可能有其他冲突的约束。如果代码不再崩溃,但视图动画仍然不正确,请将此问题标记为已解决;然后打开另一个。很好的一个,使用不同的值(999、900、500等)不是问题,真的吗?在iOS开发中是否有正常工作的东西?由于系统错误或不受支持,许多事情必须手动完成和/或必须实现大量的变通代码。抱歉,没有建设性的评论。这一限制似乎已在iOS 13中删除。我尝试将约束从
required
更改为
defaultLow
,结果成功了。与在iOS 12上崩溃时使用的代码相同。不,您不应该在
viewdilayoutsubviews
中创建约束。这个方法可以被调用多次,如果你在那里创建了重复的约束,这会使你的应用程序崩溃。酷,明白了:用999代替1000。谢谢