Drop-Down-Menu
Drop-Down-Menu copied to clipboard
Constraints crash (removeFromSuperview)
Hello, Jared!
When I want to remove button from superview (or viewWillDisappear) I have this crash: "'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x1c0673a40 "project.dropDownView:0x103155130.top"> and <NSLayoutYAxisAnchor:0x1c06736c0 "project.dropDownBtn:0x1031544a0'Select.bottom"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'"
Can You help me?
Thanks.
yes this is a bug with Jared's approach. If you implement his approach on a view that gets dismissed you get this error about the constraints not having a common ancestor. Unfortunately I don't have a solution to fix this but maybe Jared will weigh in??
to fix this you need to setup the dropdown constraints during the viewWillLayoutSubviews()
Did You mean it?
button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
button.widthAnchor.constraint(equalToConstant: 100).isActive = true
button.heightAnchor.constraint(equalToConstant: 40).isActive = true
I read about viewWillLayoutSubviews, but I could not fix it in this project
you need to move the dropdown constraints to viewWillLayoutSubviews(), not the button constraints.
these 3
dropView.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
dropView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
@dreed47 Thank you! It works)
I actually couldn't get that to work, so I actually moved those 3 constraints to the 'touchesBegan' function. Seems to work fine. Nice little add in to use throughout my app.
override
func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
dropView.topAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
dropView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
dropView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
if isOpen == false {`...