如何使用特定的宽度和高度视图设置约束

时间:2015-01-15 11:55:27

标签: ios autolayout constraints

我用可视化格式语言为我的UIImageView(一个小星星)设置了一些约束。

这是我的代码:

        //Bottom
    format = @"V:|-[_title]-|";
    constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
    [_bottom addConstraints:constraintsArray];

    format = @"V:|-[_stars(==17)]-|";
    constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
    [_bottom addConstraints:constraintsArray];

    format = @"V:|-15-[_number]-15-|";
    constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
    [_bottom addConstraints:constraintsArray];

    format = @"H:|-[_title(240)]-[_stars(17)]-[_number]-|";
    constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
    [_bottom addConstraints:constraintsArray];

请注意_bottom是我添加约束的超级视图,我已将_stars's frame设置为17 * 17。但_stars的位置不在垂直中心。

enter image description here

我该如何解决这个问题?这就是我要的。 有没有办法将视图设置为垂直中心?

enter image description here

非常感谢您的建议。

编辑: _bottom的代码。

    format = @"V:|[_top(100)][_bottom]|";
    constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
    [_background addConstraints:constraintsArray];

    format = @"H:|[_bottom]|";
    constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
    [_background addConstraints:constraintsArray];

1 个答案:

答案 0 :(得分:1)

试试这个:

[_bottom addConstraint:[NSLayoutConstraint 
                            constraintWithItem:_stars
                                     attribute:NSLayoutAttributeCenterY 
                                     relatedBy:NSLayoutRelationEqual 
                                        toItem:_bottom
                                     attribute:NSLayoutAttributeCenterY 
                                    multiplier:1.0f 
                                      constant:0.0f]];


//Bottom
format = @"V:|-[_title]-|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
[_bottom addConstraints:constraintsArray];

format = @"V:[_stars(17)]"; // @"V:|-[_stars(==17)]-|" -> @"V:[_stars(17)]"
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
[_bottom addConstraints:constraintsArray];

format = @"V:|-15-[_number]-15-|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
[_bottom addConstraints:constraintsArray];

format = @"H:|-[_title(240)]-[_stars(17)]-[_number]-|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
[_bottom addConstraints:constraintsArray];

并非所有约束都可以使用可视格式字符串创建。