4

I get the following error:

Could not resolve symbolic constant for constraint, because: Could not find relevant edges for attributes: centerX and centerX.

Use a symbolic breakpoint at NSLayoutConstraintFailedToFindDefaultResolvedValueForSymbolicConstant to debug.

If I add a breakpoint at NSLayoutConstraintFailedToFindDefaultResolvedValueForSymbolicConstant it stops at this line:

[self.customNavigationBar.widthAnchor constraintEqualToAnchor:self.view.widthAnchor].active = YES;

This line is called within the viewDidLoad of the view controller. customNavigationBar is a UIView loaded from a nib which already have been added as subview to self.view.

If I try to print out the anchors I am using everything seems ok:

(lldb) po self.customNavigationBar.widthAnchor
<NSLayoutDimension:0x17446cc80 "UIView:0x10115c160.width">

(lldb) po self.view.widthAnchor
<NSLayoutDimension:0x170667080 "UIView:0x1012ae550.width">
Nef10
  • 926
  • 2
  • 16
  • 26
  • What is `customNavigationBar`? Is it a `UINavigationBar` added as a subview to `self.view`? Or is it a subclassed controller? – DonMag Jul 13 '17 at 18:10
  • It is a UIView loaded from a nib, see edited question. – Nef10 Jul 13 '17 at 18:13
  • Just to narrow things down... If you comment-out that line, do you still get an error? If so, on a similar line? If so, can you get it to run without error if you don't set any constraints on `customNavigationBar`? (I know it won't be in the right place, but just debugging....) – DonMag Jul 13 '17 at 18:17
  • Did you end up figuring this out? I'm just now running into this problem myself and am going to be digging into the reasons in the morning. Thanks – Brian M Jan 28 '19 at 02:14
  • No, but I also didn’t look further into it. – Nef10 Jan 28 '19 at 09:57

1 Answers1

5

This error comes from your choice of constructor for the NSLayoutConstraint.

You probably have something like this:

view.topAnchor.constraint(equalToSystemSpacingBelow: otherView.centerYAnchor, multiplier: 0.25).isActive = true

But you should construct it like this:

let constraint = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: otherView, attribute: .centerY, multiplier: 0.25, constant: 1)

constraint.isActive = true
Jon Vogel
  • 5,244
  • 1
  • 39
  • 54
  • Why does the choice of initializer matter here? What's the difference between the two options? – lukas May 28 '20 at 09:20
  • The difference here is that the NSLayoutAnchor api (subtly) doesn't make any sense here. We're using `equalToSystemSpacingBelow` but there is no SystemSpacing for centers. Change that to `view.topAnchor.constraint(equalTo: otherView.centerYAnchor, multiplier: 0.25).isActive = true` and the error will do away. – alexkent Jun 03 '20 at 14:05
  • @alexkent - `multiplier` is not a valid argument for an `equalTo` constraint. It's either nothing or you can use `constant`. – C6Silver Jun 04 '20 at 03:28
  • @C6Silver you're right of course. Thanks for the correction. – alexkent Jun 07 '20 at 06:29
  • I'm not 100% sure yet but I think this answer helped me a lot... If you're right -- and it seems to -- then Apple let's you call APIs using never-legal constraints that it flags only at runtime. Grumble grumble +1 thanks – Dan Rosenstark May 11 '23 at 21:39