2

我们正在使用XcenterandYcenter NSLayoutConstraint来定位subviewparent superview。由于我们需要保持位置比例,我们不能使用constant约束的值来定位它,它需要纯粹基于乘数。

subview可以是边界之外的位置,因此superview需要一个负乘数。问题是当我们应用一个负乘数(通过代码)时,子视图的位置会以一种意想不到的方式移动。使用自动布局公式似乎是可能的,但实际上并非如此。这是一个可视化:

在此处输入图像描述

设置宽度/高度约束后,我将 X 和 Y 设置如下:

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

只有当乘数为负时,我们才会得到意想不到的结果。

有没有人暗示负乘数发生了什么?我们被困在这个问题上好几天了......

4

0 回答 0