본문 바로가기
Swift/UIKit

[UIKit] because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal Error 처리 (not addsubview)

by 마라민초닭발로제 2023. 8. 9.

Error 가 보통

because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal

으로 처리될 것이다.


 

 

UI를 선언하는 클로저가 연산 프로퍼티인지 확인하자.

많이 나오는 에러 해결 방법은 addsubview를 통해서 에러를 해결한다고 기재되어 있다.  하지만 나는 addsubview또한 정확한 언어로 해결했다. 한 두시간 얼타고 확인한 것은 클로저가 UI가 아닌 computeProperty로 정의되어 있었던 것이다.

 

//정상 코드
    var testLabel: UILabel = {
        let lb = UILabel()
        lb.text = "이거 출력 됨?"
        
        lb.translatesAutoresizingMaskIntoConstraints = false
        return lb
    }()

//연산 프로퍼티
    var testLabel: UILabel{
        let lb = UILabel()
        lb.text = "이거 출력 됨?"
        
        lb.translatesAutoresizingMaskIntoConstraints = false
        return lb
    }

따라서 문제가 발생헀던 것이다. UI선언할 때 연산 프로퍼티를 활용하지 않도록 조심하도록 하자!

 

아래는 전 동료가 준 답변


에러 전문

2023-08-08 23:22:41.987704+0900 HotCoffeeProgramtically[34029:422819] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <nslayoutyaxisanchor:0x6000021bfe40 "uitableview:0x12802ae00.top"> and <nslayoutyaxisanchor:0x6000021bf980 "uilayoutguide:0x600000ddcd20'uiviewsafearealayoutguide'.top"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'</nslayoutyaxisanchor:0x6000021bf980 "uilayoutguide:0x600000ddcd20'uiviewsafearealayoutguide'.top"></nslayoutyaxisanchor:0x6000021bfe40 "uitableview:0x12802ae00.top">