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?