I am trying to set the below constraint to one of my floating view.
leftConstraint = [NSLayoutConstraint constraintWithItem:detailView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:VIEW_WIDTH];
The view will be moved horizontally and has a fixed width. I couldn't pin this view to any other view. I will be changing the constant value in the constraint to move it over my view.
When I run the above the constraint in XCode 6.3, am getting the below error. "A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant"
I am not sure why this creates an illegal constraint.
To bypass this issue, I am using the 0.001 multiplier, but thats not going to work in all cases. So looking for an better workaround to this requirement.
This is the formula Autolayout uses:
item1.attribute1 = multiplier × item2.attribute2 + constant
So if you set multiplier to 0 or don't have an item2, attribute1 will be constant. Which is not a good idea because it's a constraint regarding to the superview without mentioning the superview. And in most cases it's the result of a bug in code. So Apple decided that it's illegal to create this kind of constraints.
Constraints without a second item are invalid for all attributes except height and width.
You should actually add your constraint explicitly in regards to the superview of the view.
Your constraint should look like this
leftConstraint = [NSLayoutConstraint constraintWithItem:detailView
attribute: NSLayoutAttributeLeft
relatedBy: NSLayoutRelationEqual
toItem: detailView.superview
attribute: NSLayoutAttributeLeft
multiplier: 1
constant: VIEW_WIDTH];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With