![](/img/trans.png)
[英]ObjC, revert to interface builder autolayout constraints, after adding / removing programmatically?
[英]Programmatically removing/adding constraints animated
我在使用动画以编程方式删除和添加约束时遇到困难。 我已将现有项目简化为一个仅用于演示的小项目。
安装程序有两个(容器)视图:顶部和底部。 它们包含两个子视图:蓝色和红色。
顶视图具有宽高比2:3约束。
我参考了我在viewDidLoad中初始化的topView,aspectRatioConstraint和heightConstraint
@IBOutlet weak var aspectRatioConstraint: NSLayoutConstraint!
var heightConstraint: NSLayoutConstraint!
@IBOutlet weak var topView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
heightConstraint = NSLayoutConstraint(item: topView, attribute: .height, relatedBy: .equal, toItem: topView, attribute: .height, multiplier: 1.0, constant: topView.frame.size.height)
NSLayoutConstraint.activate([heightConstraint])
topView.removeConstraint(aspectRatioConstraint)
}
@IBAction func startAnimation(_ sender: AnyObject) {
UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.1, options: .curveEaseIn, animations: {
self.heightConstraint.constant = 20.0
self.view.layoutIfNeeded()
}, completion: nil)
}
导航中的“+”按钮调用startAnimation方法,它应该在一段时间内调整heightConstraint的大小,但事实并非如此。
我究竟做错了什么?
示例项目 - > https://www.dropbox.com/sh/7i75i0zq9dormqb/AACzTYvWOoJL3MjcjgMIyOd9a?dl=0
由于子视图中的子视图,我对您的项目感到困惑,但我创建了一个我自己的项目,应该做您需要的。 关于您的项目的两个侧面说明:
(1)您获得的AutoLayout错误是在更改代码中的约束时。 我不知道为什么会发生这种情况......也许是因为你在彼此之间嵌套了全尺寸的子视图,或者其他东西。
(2)我会尽可能在ViewDidLoad()中更改/创建约束。 UIView真的没有它需要的东西。 我尝试在ViewWillLayoutSubviews()中进行约束更改。
这就是我所做的,基本上是你项目的一个子集:
(1)在IB中设置你能做的。 故事板中的两个视图(没有嵌套的子视图,但我认为它应该工作)。 当然,还可以连接导航控制器的添加按钮!
(2)添加以下约束:
顶视图:
- 前导,尾随和上边距为0 - 纵横比为3:2 - 高度为20
底部视图:
- 所有边距设置为0
(3)通过将顶视图高度的优先级从“必需”设置为“高”来纠正IB“错误”。
(4)为视图控制器添加方面和高度约束的IBOutlets。
(5)您在视图控制器中只需要以下代码(在您的项目中关闭)。 请注意,我在其他任何地方都没有添加任何代码:
@IBOutlet weak var heightRatio: NSLayoutConstraint!
@IBOutlet weak var aspectRatio: NSLayoutConstraint!
@IBAction func animateLayoutChange(_ sender: UIBarButtonItem) {
UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.1, options: .curveEaseIn, animations: {
self.aspectRatio.isActive = false
self.heightRatio.isActive = true
self.view.layoutIfNeeded()
}, completion: nil)
}
尝试这个:
@IBAction func startAnimation(_ sender: AnyObject) {
self.heightConstraint.constant = 20.0
UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.1, options: .curveEaseIn, animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
尝试这个 :
UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.1, options: .curveEaseIn, animations: {
NSLayoutConstraint.deactivate([self.heightConstraint])
self.heightConstraint.constant = 20.0
NSLayoutConstraint.activate([self.heightConstraint])
self.view.layoutIfNeeded()
}, completion: nil)
您在更新某些约束之前已停用
如果您只想为topView的高度设置动画,可以试试这个:
UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.1, options: .curveEaseIn, animations: {
self.topView.frame.size.height = 20.0
self.view.layoutIfNeeded()
}, completion: nil)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.