如何使用特定的宽度和高度视图设置约束
How to set constraint with a specific width and height view
我已经为我的 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
的位置不在垂直中心。
我该如何解决这个问题?这就是我要的。 有没有办法将视图设置为垂直中心?
非常感谢您的建议。
编辑: _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];
试试这个:
[_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];
并非所有约束都可以使用可视格式字符串创建。
我已经为我的 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
的位置不在垂直中心。
我该如何解决这个问题?这就是我要的。 有没有办法将视图设置为垂直中心?
非常感谢您的建议。
编辑: _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];
试试这个:
[_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];
并非所有约束都可以使用可视格式字符串创建。