【问题标题】:Update the constant property of a constraint programmatically in Swift?在 Swift 中以编程方式更新约束的常量属性?
【发布时间】:2015-01-24 15:54:50
【问题描述】:

我想为一个对象设置动画,所以我声明了一个约束并将其添加到视图中。然后我在UIView 动画中更新约束的constant 属性。为什么这段代码不移动对象?

UIView.animateWithDuration(1, animations: {
     myConstraint.constant = 0   
     self.view.updateConstraints(myConstraint)
})

【问题讨论】:

    标签: ios swift nslayoutconstraint


    【解决方案1】:

    为了声明动画,您不能重新定义约束并调用updateConstraints。您应该更改约束的constant 并遵循以下格式:

    self.view.layoutIfNeeded()
    UIView.animate(withDuration: 1) {
        self.sampleConstraint.constant = 20
        self.view.layoutIfNeeded()
    }
    

    【讨论】:

    • self.view.layoutIfNeeded() 正是我想要的!非常感谢。只是一个疑问..它是否也有某种性能影响?
    • @MuditJaju 如果不在UITableView中,一般没有性能问题。
    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 2015-06-10
    • 1970-01-01
    相关资源
    最近更新 更多