I am computing some multipliers for constraints for views created dynamically. Sometimes it is zero. When that happens, I get the following unhandled exception:
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. Location attributes must be specified in pairs
from this code:
NSLayoutConstraint(item: spanView,
attribute: .leading,
relatedBy: .equal,
toItem: self.miniOnOffView,
attribute: .trailing,
multiplier: m1,
constant: 0.0).isActive = true
Where m1 = 0, and spanView has already been added as a subview of self.miniOnOffView.
Basically, given a fraction between 0 and 1, I'm trying specify the fractional placement of the leading part of the subview. What I don't understand is why this is an error. Why is zero an illegal value?
Is there a better solution? I basically have subviews that I want to position the left and right fractionally relative to the parent, where both are anywhere between 0 and 1 of the width.
Clarification 1
@Pärserk thanks for the link. Not shooting the messenger, but Apple's statements don't entirely jive with what Interface Builder will let me do then. I've used the following as a way of position a button 3/4 with a 30 pixel bias across a container view:
I've not (yet) tried 0 for the multiplier there, but 0.75 isn't an identity value either. It's worked fine for a while.
Clarification 2
@matt, here's an example of what I'm trying to do:
Basically, I have a calendar day view. I'm trying to position the red and blue time bars based on their fractional positions within the day (the green annotations show approximate values).
I could of course make a "layout" view that implements layoutSubviews()
and do all of this proportional layout there, but I was trying to use the machinery at hand so it would be more declarative up front. I'm doing a bunch of these in a UICollectionView and the performance when I took the "just draw it all myself using draw()" was dissapointing.