约束不等于高度

时间:2018-07-05 06:30:59

标签: ios swift xcode swift4

我编写以下代码:

if !self.headerIsCollapsed{
     self.headerIsCollapsed = true
     self.heightProfilView.constant -= (self.view.bounds.height / 5)
     UIView.animate(withDuration: 0.3, animations: {
          self.imageUserProfil.layer.opacity = 0
          self.view.layoutIfNeeded()
     })
     print(self.heightProfilView.constant)
     print(self.topUserProfilView.bounds.height)
}

我的问题是:为什么两个print()的值不相同?

我需要self.topUserProfilView.bounds.height的值,然后再为另一个函数设置动画,之前是否有另一种方法来捕获此值?

谢谢

3 个答案:

答案 0 :(得分:0)

也许您有优先考虑的限制。收起该行时,请检查控制台输出,应该有一些大声的输出,其中包括约束。

也请尝试隐藏imageUserProfil

self.imageUserProfil.layer.opacity = 0
self.imageUserProfil.isHidden = true

答案 1 :(得分:0)

尝试一下:它应该可以工作

if !self.headerIsCollapsed{
    self.headerIsCollapsed = true
    self.heightProfilView.constant -= (self.view.bounds.height / 5)
    UIView.animate(withDuration: 0.3, animations: {
        self.imageUserProfil.layer.opacity = 0
        self.view.layoutIfNeeded()
    }) { (isCompleted) in
        print(self.heightProfilView.constant)
        print(self.topUserProfilView.bounds.height)
    }
    }

PS:如果您相对于任何视图高度添加heightProfilView约束,则此解决方案将不起作用,因为您在此处更改常数,但是在IB中您已分配了乘数,因此在这种情况下默认常数值为0。 ,这种方法不适用于相对高度限制。因为我们无法更改约束的乘数。

答案 2 :(得分:0)

您需要完成块...只需替换如下的动画方法

 UIView.animate(withDuration: 0.3, animations: {
        self.imageUserProfil.layer.opacity = 0
        self.view.layoutIfNeeded()
    }) { (isDone) in
        print(self.heightProfilView.constant)
        print(self.topUserProfilView.bounds.height)
    }

希望它会起作用。