[英]Swift - constraint or its anchors reference items in different view hierarchies
I am getting this error message:我收到此错误消息:
'Unable to activate constraint with anchors and because they have no common ancestor. '无法使用锚点激活约束,因为它们没有共同的祖先。 Does the constraint or its anchors reference items in different view hierarchies?约束或其锚点是否引用了不同视图层次结构中的项? That's illegal.'那是非法的。
I basically know what the problem is but I have no idea how to fix it.我基本上知道问题是什么,但我不知道如何解决它。 I am trying to dismiss views
like this:我试图驳回这样的views
:
func dismissPopUpView(){
UIView.animate(withDuration: 0.3, animations: {
self.popUpView.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.wishButton.alpha = 0
self.popUpView.alpha = 0
self.visualEffectView.alpha = 0
self.dropDownButton.alpha = 0
self.closeButton.alpha = 0
}) { (_) in
self.dropDownButton.dismissDropDown()
self.dropDownButton.dropView.removeFromSuperview()
self.dropDownButton.removeFromSuperview()
self.wishButton.removeFromSuperview()
self.visualEffectView.removeFromSuperview()
self.closeButton.removeFromSuperview()
self.popUpView.removeFromSuperview()
}
}
The problem is that dropDownButton.dropView
is created within the dropDownButton
- class
like this:问题是, dropDownButton.dropView
是内创建dropDownButton
- class
是这样的:
override func didMoveToSuperview() {
self.superview?.addSubview(dropView)
self.superview?.bringSubviewToFront(dropView)
dropView.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
dropView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
height = dropView.heightAnchor.constraint(equalToConstant: 0)
}
var isOpen = false
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if isOpen == false {
isOpen = true
NSLayoutConstraint.deactivate([self.height])
if self.dropView.tableView.contentSize.height > 150 {
self.height.constant = 150
} else {
self.height.constant = self.dropView.tableView.contentSize.height
}
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
self.dropView.layoutIfNeeded()
self.dropView.center.y += self.dropView.frame.height / 2
}, completion: nil)
} else {
isOpen = false
NSLayoutConstraint.deactivate([self.height])
self.height.constant = 0
NSLayoutConstraint.activate([self.height])
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
self.dropView.center.y -= self.dropView.frame.height / 2
self.dropView.layoutIfNeeded()
}, completion: nil)
}
}
I thought dismissing the dropView
first and then dropDownButton
will solve the problem but it doesn't.我认为首先关闭dropView
然后dropDownButton
会解决问题,但事实并非如此。 Anyone know how I can fix this?有谁知道我该如何解决这个问题?
I finally got it working.我终于让它工作了。 I added this inside my dropDownBtn
- class
:我在dropDownBtn
- class
添加了这个:
override func removeFromSuperview() {
dropView.bottomAnchor.constraint(equalTo: self.topAnchor).isActive = false
dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = false
dropView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = false
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.