1

我只是在摆弄 AutoLayout(使用代码),遇到了一些我不太了解的东西。我做了一个简单的例子来演示这个问题:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //create views
    UIView *redView = [UIView new];
    redView.backgroundColor = [UIColor redColor];
    redView.translatesAutoresizingMaskIntoConstraints = NO;

    UIView *blueView = [UIView new];
    blueView.backgroundColor = [UIColor blueColor];
    blueView.translatesAutoresizingMaskIntoConstraints = NO;

    //add views to viewcontroller's view
    [self.view addSubview:redView];
    [self.view addSubview:blueView];

    //add contraints
    NSDictionary *views = NSDictionaryOfVariableBindings(redView, blueView);

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[redView]-|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[blueView]-|" options:0 metrics:nil views:views]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[redView]-[blueView]-|" options:0 metrics:nil views:views]];
}

我希望得到以下结果:http: //i.stack.imgur.com/LCWQP.png

但相反,它给出了以下结果:http: //i.stack.imgur.com/DJK5i.png

现在,我知道它可以通过使用来解决[NSLayoutConstraint constraintWithItem: attribute: relatedBy: toItem: attribute: multiplier: constant: ],但我想知道是否有一个 VisualFormat 选项,或者我的代码中缺少一些东西......

提前感谢您在我的学习过程中的帮助!

4

1 回答 1

0
@"V:|-[redView]-[blueView]-|"

这表示 redView 的底部应该与 blueView 的顶部对齐,它就是这样。它没有说明 redView 和 blueView 的相对高度。

如果你想让它们的高度相等,你可以这么说。

@"V:|-[redView]-[blueView(==redView)]-|"
于 2014-03-04T16:30:07.300 回答