【发布时间】:2017-09-25 19:47:00
【问题描述】:
我正在尝试将一个按钮从UIViewController 的可见边界之外移动到它的中心。我的情节提要中有一个名为 myButtonLeading 的约束设置,我将其作为动画的一部分删除,然后我想添加一个名为 newCenterConstraint 的新约束。
@IBAction func updateDidTouch(_ sender: Any) {
UIView.animate(withDuration: 0.5, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
let newCenterConstraint = NSLayoutConstraint(item: self.myButton, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0.0)
self.myButton.removeConstraint(self.myButtonLeading)
self.myButton.addConstraint(newCenterConstraint)
self.view.layoutIfNeeded()
}, completion: nil)
}
我现在的代码给出了以下关于我引用 toItem: view 的错误消息。
在闭包中隐式使用“self”;使用“自我”。捕捉 语义显式
但是当我使用self.view 时,我的应用程序崩溃并显示错误消息:
无法在视图上安装约束。是否约束参考 视图子树之外的东西?这是违法的。
我在添加新的 centerX 约束时哪里出错了?
【问题讨论】:
-
按钮的superview和self.view有关系吗?
标签: ios swift nslayoutconstraint uiviewanimation