设置后NSLayoutConstraint常量不更新 [英] NSLayoutConstraint constant not updating after setting

查看:69
本文介绍了设置后NSLayoutConstraint常量不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我正在尝试在按下按钮后更新常量.但是常量似乎在设置后不会更新.即使将其设置为-500,它仍然记录0.我正在呼叫 layoutIfNeeded ,但是什么也没发生.

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 layoutIfNeeded but nothing happens.

// 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
    }

更新

当我想第二次使用它时,我的NSLayoutConstraint似乎为(null).那可以解释为什么它没有更新.我应该如何对其进行引用?

推荐答案

您将需要调用存在NSLayoutConstraint的相应 UIView(control) setNeedsUpdateConstraints 方法更新约束.

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

例如UIButton

For example of UIButton

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

以您的情况

[self setNeedsUpdateConstraints];

这篇关于设置后NSLayoutConstraint常量不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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