Autolayout:无法布置两个视图,一个左对齐,另一个右对齐

时间:2013-09-09 14:37:25

标签: ios autolayout nslayoutconstraint

我正面临着autolayout的奇怪行为。我想向viewcontroller添加一个容器视图,然后添加两个标签:一个是左边对齐容器的左边,而另一个是右边对齐容器的右边。 左标签正确布局但右标签没有。我刚刚发现添加了20个点,右侧标签右侧与父级左边缘对齐,而不是右侧!为什么?我做错了什么?

{
NSArray *cons = nil;
NSLayoutConstraint *cc = nil;

UIView *container = [[UIView alloc]init];
[container setTranslatesAutoresizingMaskIntoConstraints:NO];
[container setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:container];

cons = [NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[view]-0-|"
                                               options: 0
                                               metrics:nil
                                                 views:@{@"view" : container}];

for (NSLayoutConstraint *con in  cons)
    [self.view addConstraint:con];

cons = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[view]-0-|"
                                               options: 0
                                               metrics:nil
                                                 views:@{@"view" : container}];

for (NSLayoutConstraint *con in  cons)
    [self.view addConstraint:con];



UILabel *currentLabel = [[UILabel alloc]init];
[currentLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[currentLabel setText:@"Punti attuali"];
[currentLabel setBackgroundColor:[UIColor grayColor]];
[container addSubview:currentLabel];


cc = [NSLayoutConstraint constraintWithItem:currentLabel
                                  attribute:NSLayoutAttributeLeft
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:container
                                  attribute:NSLayoutAttributeLeft
                                 multiplier:1.f constant:0.f];

[container addConstraint:cc];


cc = [NSLayoutConstraint constraintWithItem:currentLabel
                                  attribute:NSLayoutAttributeTop
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:container
                                  attribute:NSLayoutAttributeTop
                                 multiplier:1.f constant:0.f];

[container addConstraint:cc];



UILabel *currentPoint = [[UILabel alloc]init];
[currentPoint setTranslatesAutoresizingMaskIntoConstraints:NO];
[currentPoint setText:@"100"];
[currentPoint setBackgroundColor:[UIColor greenColor]];
[container addSubview:currentPoint];

cc = [NSLayoutConstraint constraintWithItem:currentPoint
                                  attribute:NSLayoutAttributeRight
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:container
                                  attribute:NSLayoutAttributeRight
                                 multiplier:0.f constant:20.f];


[container addConstraint:cc];

cc = [NSLayoutConstraint constraintWithItem:currentPoint
                                  attribute:NSLayoutAttributeTop
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:container
                                  attribute:NSLayoutAttributeTop
                                 multiplier:1.f constant:0.f];

[container addConstraint:cc];

}

这应该是一项非常重要的任务,但我花了一个小时才能找到解决方案。

0 个答案:

没有答案