NSLayoutConstraint negative multiplier gives unexpected results

281 Views Asked by At

We are using Xcenter and Ycenter NSLayoutConstraint to position subview inside parent superview. As we need to keep the position proportional we can't use the constant value of the constraints to position it, it needs to be based purely on the multiplier.

The subview can be a position outside of the superview bounds so a negative multiplier is needed. The thing is that when we apply a negative multiplier (via code) the subview position is moving in an unexpected way. Using the auto layout formula it seems to be possible, but in practice, it's not. Here is a visualization:

enter image description here

After Setting the width/height constraints I'm settings the X and Y ones like this:

let xAnchor = NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: superview, attribute: .centerX, multiplier: multpliers.x, constant: 0)        
xAnchor.isActive = true

let yAnchor = NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: superview, attribute: .centerY, multiplier: multpliers.y, constant: 0)
yAnchor.isActive = true

Only when the multiplier is negative we get unexpected results.

Does anyone has a hint abound what's going on with negative multipliers? We are stuck on this for a few good old days...

0

There are 0 best solutions below