简体   繁体   English

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

[英]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.

相关问题 如何设置视图的最大高度限制? - How to set max height constraint for a view? 如何将视图高度设置为等于其宽度 - How to set a view height to be equal to its width 接口构建器中的约束“width equals height”,对于相同的视图:如何创建这样的约束? - Constraint “width equals height” in interface builder, for the same view: how to create such a constraint? 如何在Xcode 10中为对象设置高度和宽度? 如果设置值,则表示固定约束可能会导致削波 - How to set Height and width for a object in Xcode 10? If we set value it says fixed constraint may cause clipping 如何在UIButton上设置宽度约束 - How to set a width constraint on a UIButton 如何在瀑布视图中自动设置图像的宽度和高度? - How to set image width and height automatically in a waterfall view? 如何在iOS中的autolayout中设置视图的动态宽度和高度? - How to set dynamic width & height of a view in autolayout in iOS? 自动布局约束:如何在调整大小时使视图保持其宽高比? - Auto-Layout Constraint: How to make a view maintains its width/height ratio when resized? ImageView限制宽度但不限制高度 - ImageView to constraint the width but not the height Xcode:在实用程序/大小/视图中设置宽度和高度,而不是作为约束 - Xcode: Setting width and height in Utilities / Size / View versus as a constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM