Drop-Down-Menu icon indicating copy to clipboard operation
Drop-Down-Menu copied to clipboard

Constraints crash (removeFromSuperview)

Open SonnyMore opened this issue 6 years ago • 6 comments

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.

SonnyMore avatar Nov 24 '17 09:11 SonnyMore

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??

dreed47 avatar Dec 05 '17 16:12 dreed47

to fix this you need to setup the dropdown constraints during the viewWillLayoutSubviews()

dreed47 avatar Dec 05 '17 18:12 dreed47

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

SonnyMore avatar Dec 11 '17 10:12 SonnyMore

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 avatar Dec 12 '17 13:12 dreed47

@dreed47 Thank you! It works)

SonnyMore avatar Dec 17 '17 14:12 SonnyMore

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 {`...

mashype avatar Mar 02 '18 08:03 mashype