gpt4 book ai didi

ios - Swift - 约束或其 anchor 引用不同 View 层次结构中的项目

转载 作者:行者123 更新时间:2023-11-29 05:13:54 25 4
gpt4 key购买 nike

我收到此错误消息:

'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.'

我基本上知道问题是什么,但不知道如何解决。我试图像这样消除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()
}
}

问题是 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)

}
}

我认为先关闭 dropView 然后再关闭 dropDownButton 可以解决问题,但事实并非如此。有人知道我该如何解决这个问题吗?

最佳答案

我终于成功了。我将其添加到我的 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
}

关于ios - Swift - 约束或其 anchor 引用不同 View 层次结构中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59392170/

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