because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies?
override func viewDidLoad() {
super.viewDidLoad()
let myContainer = UIView()
myContainer.backgroundColor = .purple
myContainer.translatesAutoresizingMaskIntoConstraints = false
myContainer.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
myContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myContainer.widthAnchor.constraint(equalToConstant: 200).isActive = true
myContainer.heightAnchor.constraint(equalToConstant: 200).isActive = true
self.view.addSubview(myContainer)
}
ERROR:
2019-11-06 20:06:17.763701-0600 Testing AutoLayout[42202:4013241] *** Terminating app due to uncaught exception 'NSGenericException', reason: '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.'
If I comment the following two lines the error goes away and myContainer
view shows in the upper-left corner.
//myContainer.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
//myContainer.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
Any idea why the error if 'myContainer' is the only thing I have?