[英]How to set constraint with a specific width and height view
I had set some constraints with visual format language to my UIImageView( a little star). 我对UIImageView(有点小星星)的视觉格式语言设置了一些限制。
Here is my code: 这是我的代码:
//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];
Note that the _bottom is the super view where I added constraints and I had set the _stars's frame
to 17 * 17
. 请注意,_bottom是超级视图,我在其中添加了约束,并且已将
_stars's frame
设置为17 * 17
。 But the position of the _stars
is not at the vertical center. 但是
_stars
的位置不在垂直中心。
How can I fix this? 我怎样才能解决这个问题? This is what i want.
这就是我要的。 Is there a way to set the view to vertival center?
有没有一种方法可以将视图设置为椎体中心?
Thanks a lot for advice. 非常感谢您的建议。
Edit: _bottom 's code. 编辑: _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];
try this: 尝试这个:
[_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];
Not all constraints can be created with visual format string. 并非所有约束都可以使用可视格式字符串创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.