When I develop an iOS app, I add a UITextField in the app UI programmatically. And I also add some constraints to the UITextField object in swift source code to make it auto layout. But when I run the app, it throws an exception with the error message ‘NSGenericException’, reason: ‘Unable to activate constraint with anchors, because they have no common ancestor like below.
1. The Detailed Error Messages.
- Below are the detailed error messages of this NSGenericException error.
Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x600000f99340 "UITextField:0x7fd8b7404bd0.top"> and <NSLayoutYAxisAnchor:0x600000f99380 "UIView:0x7fd8b760e140.top"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
2. How To Fix The NSGenericException Error.
- After some time investigating, I finally find the method to fix this error.
- The error is because I do not add the programmatically created UITextField object into the iOS app root view’s subview hierarchy.
- So I need to use the below code to add it as the iOS app root view’s subview self.view.addSubview(urlTextBox).
- Below is the full source code.
// // ViewController.swift // iOSWebViewExample // // Created by song zhao on 11/28/19. // Copyright © 2019 dev2qa.com. All rights reserved. // import UIKit import WebKit class ViewController: UIViewController { // First create an instance of UITextField class. let urlTextBox:UITextField = UITextField() override func viewDidLoad() { super.viewDidLoad() initUrlTextBox() } func initUrlTextBox(){ /* Add the UITextField as the subview of the app root view. This line of code can avoid 'NSGenericException', reason: 'Unable to activate constraint with anchors because they have no common ancestor error when adding constraints to the UITextField object later. */ self.view.addSubview(urlTextBox1) /* Set the UITextField object's translatesAutoresizingMaskIntoConstraints property's value to false. This can avoid [LayoutConstraints] Unable to simultaneously satisfy constraints error when add constraints to the UITextField object later. */ urlTextBox.translatesAutoresizingMaskIntoConstraints = false // Set the UITextField object's text. urlTextBox.text = "Hello World!" // Set the UITextField object's backgroud color. urlTextBox.backgroundColor = UIColor.green // Create and activate top anchor constraint, center X anchor constraint and width anchor constraint. let urlTextBoxTopAnchor = urlTextBox.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 500) urlTextBoxTopAnchor.isActive = true let urlTextBoxCenterXAnchor = urlTextBox.centerXAnchor.constraint(equalTo: self.view.centerXAnchor) urlTextBoxCenterXAnchor.isActive = true let urlTextBoxWidthAnchor = urlTextBox.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.8) urlTextBoxWidthAnchor.isActive = true } }
- The above source code also fixes another error which has the following error message.
[LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want.
- This error is because I do not set the UITextField’s property translatesAutoresizingMaskIntoConstraints value to false before adding constraints to the UITextField object.
- So we should first set the UI component’s property translatesAutoresizingMaskIntoConstraints to false before add constraints to it like this urlTextBox.translatesAutoresizingMaskIntoConstraints = false.
3. How To Fix The Post-update Error: Unable To Activate Constraint With Anchors In Xcode 11.5.
3.1 Question.
- My iOS app throws the error Post-Update Error: Unable to activate constraint with anchors when I upgrade my Xcode to version 11.5.
- There is a TabBar with 6 tab items in it, each tab item contains a view object, when I click one of the tab views, it shows the below error message.
Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x60000064b4c0 "UILabel:0x7fb6dd8b8b80'Day 1'.centerY"> and <NSLayoutYAxisAnchor:0x600000649780 "UIView:0x7fb6dd4b89d0.centerY"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
- There is a segControl object in the view and the object is used to update a TableView‘s data by pulling the data from the internet.
- I do not change any code, just upgrade my Xcode to version 11.5. And maybe there are some constraints conflicting between labels and views, but I can not figure them out.
- Can anyone tell me how to fix this issue? Thanks a lot.
3.2 Answer1.
- The reason for this issue is becaue maybe you have modified the IBOutlet property variable names.
- You can read the article How To Rename IBOutlet Variable Or IBAction Function Without Lose Connection In Swift to learn more.
nice tutorial