我有一个带有2个子视图的UIScrollView.我希望一个子视图是"前导对齐"(左对齐),其前缘与滚动视图的前缘对齐.我希望其他子视图为"尾对齐"(右对齐),其后缘与滚动视图的后缘对齐.
由于某种原因,autolayout意外地将第二个尾随对齐的子视图放在滚动视图的边界之外,到另一个子视图的前(左)侧,这样子视图的后边缘与滚动视图的前沿对齐.
我正在尝试以编程方式执行此操作.代码如下.我为2个子视图使用了2个标签."alpha"标签是正确的前导对齐,但"beta"标签不是应该是尾随对齐的.
如果我尝试使用左对齐和右对齐而不是前导和尾随,也会发生这种情况.右对齐标签显示在与尾随对齐标签相同的错误位置.
我已经在这里和其他地方多次阅读iOS 6发行说明和答案,我只是不确定为什么会发生这种情况.
在视图控制器中:
- (void) viewDidLoad
{
[super viewDidLoad];
// Create and configure the scroll view.
UIScrollView * scrollView = [[UIScrollView alloc] init];
[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
// For debugging.
[scrollView setClipsToBounds:NO];
scrollView.layer.borderColor = [UIColor redColor].CGColor;
scrollView.layer.borderWidth = 1.0;
[[self view] addSubview:scrollView];
// Layout scrollview.
// Horizontal: leading edge to superview's leading edge, with indent.
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:20.0]];
// Horizontal: trailing edge to superview's trailing edge, with indent.
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
attribute:NSLayoutAttributeTrailing …Run Code Online (Sandbox Code Playgroud)