I add a UILabel
(amountLabel) in UIViewController
in storyboard editor. And then in swift file, viewDidLoad
, I programatically create a
let nameLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
if you forgot setting this label.translatesAutoresizingMaskIntoConstraints = false
or if you add subview after constraints
this problem arises
I ran into the same problem that you described earlier. In order to make the programmatic subview, (in your case the paymentTextField) you have to add this to the subview first and then apply your constraints.
By adding the subview to view first, this ensures both views have the same parent.
Make sure you added the paymentTextField
to your view:
paymentTextField.translatesAutoResizingMaskIntoConstraints = false
view.addSubview(paymentTextField)
...
Add your constraints, for example paymentTextField.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
Checklist for this ISSUE:
The error states that "because they have no common ancestor", which means that they don't share the same parent. In order to relate constraint between two items, they have to have a child-parent relationship or a sibling one.
In your case just make sure they have the same parent view before adding the constraint programmatically.