How To Fix Swift Error Unable To Activate Constraint With Anchors Because They Have No Common Ancestor & LayoutConstraints Unable To Simultaneously Satisfy Constraints

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 error message ‘NSGenericException’, reason: ‘Unable to activate constraint with anchors, because they have no common ancestor like below.

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.’

After sometime investigate, i finally find the method to fix this error. This 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 below code to add it as the iOS app root view’s subview self.view.addSubview(urlTextBox). Below is full source code.

Below source code also fix another error which has 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 add constraints to the UITextField object.

So we should first set the UI component’s property translatesAutoresizingMaskIntoConstraints to false before add constraints to it. urlTextBox.translatesAutoresizingMaskIntoConstraints = false

//
//  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 add 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
       
    }
    
}

READ :   How To Fix Xcode Error Failed To Create Provisioning Profile. There Are No Devices Registered In Your Account On The Developer Website.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.