iosswiftios-autolayout

Unable to activate because they have no common ancestor IBOutlet and code


I am trying to add a constraint to a tableview I created and added to view programatically. I want to constraint it based on a UITextField which is an IBOutlet. However, I am getting the following error:

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

@IBOutlet weak var authorTextField: UITextField!

override func viewDidLoad() {
    myTableView = MyTableView(frame: CGRect(x: 0, y: 80, width: 320, height: 120), style: .plain)
    myTableView!.isHidden = false
    myTableView!.backgroundColor = UIColor.green
    self.view.addSubview(myTableView!)
    myTableView.setConstraints(to: authorTextField)  // <-- this fails

// MyTableView.swift ..
func setConstraints(to view: UIView)
    self.translatesAutoresizingMaskIntoConstraints = false
    self.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8.0).isActive = true
    self.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8.0).isActive = true
}

How to add this constraint?


Solution

  • The answer is in the error message. Your property authorTextField is in another view hierarchy. Go into your storyboard and put the textfield in the same view. On another note, dont mix setting frames and autolayout constraints, i think your confusing yourself here. Instead of

    MyTableView(frame: CGRect(x: 0, y: 80, width: 320, height: 120), style: .plain)

    write

    MyTableView(frame: CGRect.zero, style: .plain)

    and add a height constraint to your setConstraints method

    self.view.heightAnchor.constraint(equalToConstant: 120).isActive = true