ios NSLayoutConstraint 常量在设置后不更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25241191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:47:58  来源:igfitidea点击:

NSLayoutConstraint constant not updating after setting

iosobjective-cuiviewanimationnslayoutconstraint

提问by Ramin Afshar

I have a UIViewsubclass with a corresponding xibfile. In my xib I have a NSLayoutConstraintproperty which I'm trying to animate. I have an animateInmethod. The problem is that only the animateIn method works. When I try to update the constant again it simply stays at the previous value.

我有一个UIView带有相应xib文件的子类。在我的 xib 中,我有一个NSLayoutConstraint我想要制作动画的属性。我有一个animateIn方法。问题是只有 animateIn 方法有效。当我尝试再次更新常量时,它只是停留在以前的值。

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *horizontalConstraint;

I'm trying to update the constant after a button press. But the constant doesn't seem to update after setting. It still logs 0 even after setting it to -500. I'm calling layoutIfNeededbut nothing happens.

我试图在按下按钮后更新常量。但是设置后常量似乎没有更新。即使将其设置为 -500,它仍然记录 0。我正在打电话,layoutIfNeeded但没有任何反应。

// this works
- (void) animateIn { 
    [UIView animateWithDuration:1.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alpha = 1.0;
    } completion:^(BOOL finished) {

        [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.horizontalConstraint.constant = 0;
            [self layoutIfNeeded];
        } completion:^(BOOL finished) {
        }];
    }];
}

// this does not work
- (IBAction)resume:(id)sender {
        self.horizontalConstraint.constant = -500;
        [self layoutIfNeeded];

        NSLog(@"%f",self.horizontalConstraint.constant); // this always stays 0
    }

UPDATE

更新

It seems that my NSLayoutConstraint is (null) when I want to use it a second time. That would explain why it is not updating. How should I keep a reference to it?

当我想第二次使用它时,我的 NSLayoutConstraint 似乎是 (null)。这将解释为什么它没有更新。我应该如何保留对它的引用?

回答by Yogesh Suthar

You will need to call setNeedsUpdateConstraintsmethod of corresponding UIView(control)of which your NSLayoutConstraint is present to update constraint.

您将需要调用setNeedsUpdateConstraints对应UIView(control)的方法,其中您的 NSLayoutConstraint 存在以更新约束。

For example of UIButton

以 UIButton 为例

self.buttonConstraint.constant = 55;
[self.btnTest setNeedsUpdateConstraints];

In your case

在你的情况下

[self setNeedsUpdateConstraints];

回答by Frédéric Adda

Are you sure this constraint is not de-activated somewhere? Setting the "active" property of a constraint to false causes the view hierarchy to remove the constraint. If you don't also have a reference to it, the constraint object will be deleted from memory.

您确定此约束未在某处停用吗?将约束的“active”属性设置为 false 会导致视图层次结构移除约束。如果您也没有对它的引用,则约束对象将从内存中删除。

I had the same issue as you, and removed the "weak" so the constraint is now a strong property. Consequently, it's not set to nil when de-activated (because my view controller always has a strong pointer to it), and I can re-activate it and re-set its constant.

我和你有同样的问题,并删除了“弱”,所以约束现在是一个强属性。因此,它在停用时不会设置为 nil(因为我的视图控制器总是有一个指向它的强指针),我可以重新激活它并重新设置它的常量。

// NB: don't create these contraints outlets as "weak" if you intend to de-activate them, otherwise they would be set to nil when deactivated!
@IBOutlet private var captionViewHeightConstraint: NSLayoutConstraint!

回答by CLK

I ran into a problem where the first time I set the constraint constant in code the constant would be set but then after calling layoutIfNeeded(), such as this:

我遇到了一个问题,我第一次在代码中设置约束常量时会设置常量,但是在调用 layoutIfNeeded() 之后,例如:

myConstraint.constant = newValue;
[view layoutIfNeeded]

the constant would go back to the original value! I could watch the value change back in the debugger.

常数会回到原来的值!我可以在调试器中看到值的变化。

I finally figured out that it was because, in the storyboard, I had set a variation on the constant. For example:

我终于发现这是因为,在情节提要中,我设置了常量的变体。例如:

enter image description here

在此处输入图片说明

As soon as I removed the variation, the constraint constant value did not change back to the original value with layoutIfNeeded call.

一旦我删除了变化,约束常量值就不会通过 layoutIfNeeded 调用更改回原始值。

回答by Bogdan Somlea

when you finished your constraint changes just call:

完成约束更改后,只需调用:

[self setNeedsUpdateConstraints];

回答by Nikolay Markov

The horizontalConstraint property should be holded by the view itself. As long as the view is alive the property should be not nil (unless you set it to nil explicitly somewhere else in the code). Double check whether you set it to nil by mistake.

horizo​​ntalConstraint 属性应该由视图本身持有。只要视图处于活动状态,该属性就不应为零(除非您在代码中的其他地方将其显式设置为 nil)。仔细检查您是否错误地将其设置为 nil。