gpt4 book ai didi

swift - 如何从容器 View 中为父 View Controller 中的约束更改设置动画? ( swift 4)

转载 作者:行者123 更新时间:2023-11-28 15:27:33 26 4
gpt4 key购买 nike

我在 View Controller 中有一个容器 View ,其中包含一个按钮,每次按下时都会在父 View Controller 中向下移动标签。我通过 prepare for segue 将标签的约束传递给容器。从那里我可以修改约束,但它不会动画。

我应该怎么做?

visual example

class ViewController: UIViewController {

@IBOutlet weak var LabelTopConstraint: NSLayoutConstraint!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "containerSegue" {
let container = segue.destination as? Container
container?.LabelTopConstraint = LabelTopConstraint
}
}
}

class Container: UIViewController {

weak var LabelTopConstraint: NSLayoutConstraint!

@IBAction func MoveLabelButtonPressed(_ sender: Any) {
LabelTopConstraint.constant += 30
UIView.animate(withDuration: 0.35, animations: {
self.view.layoutIfNeeded()
})
}
}

最佳答案

100% 有效的答案你需要更新父 View 的布局,比如..

@IBAction func MoveLabelButtonPressed(_ sender: Any) {


self.LabelTopConstraint.constant += 30 // Your constraint constant change


UIView.animate(withDuration: 3.0) {
self.parent?.view.layoutIfNeeded() // Force lays of all subviews on parent view .
}

}

输出:

enter image description here

关于swift - 如何从容器 View 中为父 View Controller 中的约束更改设置动画? ( swift 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45024899/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com