Potential bug with "Unable to activate constraint with anchors"

The ios app I am currently developing starts off on a login screen which then presents the next view controller via a login button. Upon moving on to the next VC I get the error:

Terminating app due to uncaught exception 'NSGenericException', 
reason: 'Unable to activate constraint with anchors 
<NSLayoutYAxisAnchor:0x600000d0af40 "UIStackView:0x7fe87df14160.top"> and
<NSLayoutYAxisAnchor:0x600000d0ad40 "UILayoutGuide:0x600002102300'UIViewSafeAreaLayoutGuide'.bottom"> 
because they have no common ancestor.  
Does the constraint or its anchors reference items
in different view hierarchies?  That's illegal.'

Here is the relevant VC code for the contoller being showed:

class LocationSelectViewController: UIViewController {
   
  let promptLabel: UILabel = {
    let label = UILabel()
     
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "Enter Your Location`s \nAddress"
    label.font = .boldSystemFont(ofSize: 23)
    label.numberOfLines = 0
    label.preferredMaxLayoutWidth = label.frame.width
    label.textAlignment = .center
     
    return label
  }()
   
  let streetAddressField: UITextField = {
    let textField = UITextField()
   
    textField.translatesAutoresizingMaskIntoConstraints = false
    textField.placeholder = "street address" 
    textField.borderStyle = .roundedRect
    textField.textAlignment = .left
   
    return textField
  }()
   
  let cityField: UITextField = {
    let textField = UITextField()
   
    textField.translatesAutoresizingMaskIntoConstraints = false
    textField.placeholder = "city"
    textField.borderStyle = .roundedRect
    textField.textAlignment = .left
   
    return textField
  }()
   
  let stateField: UITextField = {
    let textField = UITextField()
   
    textField.translatesAutoresizingMaskIntoConstraints = false
    textField.placeholder = "state"
    textField.borderStyle = .roundedRect
    textField.textAlignment = .left
   
    return textField
  }()
   
  let saveButton: UIButton = {
    let button = UIButton(type: .system)
    button.setTitle("Add Location", for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
     
    return button
  }()
   
  var currentUser: User?
  let geocoder = CLGeocoder()

  override func viewDidLoad() {
    super.viewDidLoad()
     
    view.backgroundColor = .white
     
    let stackView = UIStackView()
     
    let views: [UIView] = [streetAddressField, cityField, stateField]
     
    stackView.axis = .vertical
    stackView.spacing = 17
    stackView.translatesAutoresizingMaskIntoConstraints = false
     
    view.addSubview(promptLabel)
    view.addSubview(stackView)
    view.addSubview(saveButton)
     
    for view in views {
      stackView.addArrangedSubview(view)
    }
     
    let constraintsArray: [NSLayoutConstraint] = [promptLabel.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor), promptLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30), promptLabel.heightAnchor.constraint(lessThanOrEqualToConstant: 70), stackView.topAnchor.constraint(equalTo: promptLabel.bottomAnchor, constant: 40), stackView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor), stackView.widthAnchor.constraint(equalToConstant: 300), saveButton.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor), saveButton.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 30)]
     
    saveButton.addTarget(self, action: #selector(addLocationTouchUpInside), for: .touchUpInside)
     
    NSLayoutConstraint.activate(constraintsArray)
  }

Also I am aware that the constraints are hard to read so they are listed below

promptLabel.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor), 

promptLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30), 

promptLabel.heightAnchor.constraint(lessThanOrEqualToConstant: 70), 

stackView.topAnchor.constraint(equalTo: promptLabel.bottomAnchor, constant: 40), 

stackView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor), 

stackView.widthAnchor.constraint(equalToConstant: 300), saveButton.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor), 

saveButton.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 30)

I orginally asked this question on SO which has lead me to believe this is a potential bug.

I likely could hold off or workaround the issue by rewriting/choosing different constraints, but if its a bug I would much rather not put in pointless work.

  • Am having a similar issue, and I lean towards thinking it's a bug

Add a Comment