奇怪的 NSLayoutConstraint 行为

Strange NSLayoutConstraint Behavior

我有一个 UIScrollView,我向其添加了一个 UITextView。我有以下约束,我也将其添加到视图中:

NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:textView2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_scrollView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:50];

此约束非常有效,但像这样将其从前导更改为尾随却没有任何效果。它仍然基于领先显示。

NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:textView2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:50];

我不明白为什么当我将约束从前导更改为尾随时,UITextView 的位置没有改变。我意识到,如果它限制在后缘,我还需要将 50 更改为 -50,但事实并非如此。

这个问题的解决方案原来是在 UIScrollView 中插入一个 UIView。我在情节提要中这样做,并将 UIView 的顶部和底部约束设置为 UIScrollView,但我将 UIView 的前导和尾随约束设置为包含 UIScrollView 的父视图,而 UIScrollView 又包含 UIView。这允许前导和尾随约束正常运行。