简体   繁体   English

以编程方式将视图的前沿与约束对齐

[英]Align Leading Edges of View with constraints programmatically

I am having an UIImageView (say imageView1 ) and a UITextView (say textView1 ) which have to be displayed vertically (one [ imageView1 ] below the other [ textView1 ]) beginning with the same margin position as of textView1 . 我具有UIImageView (比方说imageView1)UITextView (比方说textView1),其具有被垂直显示(低于其他[textView1][imageView1])具有相同的边际位置作为textView1的开始。 I have to achieve this through autolayout programmatically. 我必须通过编程自动布局来实现这一点。

I know that this can be done by setting the vertical constraints like below for both the views. 我知道可以通过设置两个视图的垂直约束来完成此操作。

NSLayoutConstraint constraintsWithVisualFormat:@"V:|[textView1]"

But the problem I have here is I already have many text views( textView2 , textView3 ) arranged in horizontal before and after this textView1 . 但是我这里的问题是,在此textView1之前和之后,我已经有许多水平排列的文本视图( textView2textView3 )。

I have already added many autolayout constraints to this textView1 through storyboard. 我已经通过故事板增加了许多自动布局约束这个textView1。 Based on the different screen size and orientation the textView1 margin differs as per the constraints that are provided on the storyboard for this. 根据不同的屏幕尺寸和方向, textView1页边距会根据情节提要上为此提供的约束而有所不同。

Now how can I provide the autolayout constraint programmatically in such a way that my imageView1 is to align in par vertically with the same margin as that of textView1 ? 现在,我怎么编程方式提供自动布局约束以这样的方式,我imageView1是在标准杆垂直同样幅度为textView1的一致?

ps: imageView1 is created programmatically in code but where as all other views that I mentioned above are created through storyboard. ps: imageView1是通过代码以编程方式创建的,但正如我上面提到的所有其他视图都是通过情节提要创建的。

+ Adding images for easy understanding +添加图像以便于理解

在此处输入图片说明

In the image, imageView1 is the UI Image. 在图像中, imageView1UI图像。 I have created it in storyboard just for understanding purpose but in real it will be created programmatically and this have to be aligned to the margin of UITextView ( textView1 ) present below it. 我已经在情节提要中创建了它,只是为了理解目的,但实际上它将以编程方式创建,并且必须与它下面显示的UITextViewtextView1 )的边距对齐。

在此处输入图片说明

This is the constraint that I want to create it through programmatically(In case this is the real question here :). 这是我想以编程方式创建它的约束(以防万一,这是真正的问题:)。

This constraint is to always make sure that imageView1 and textView1 start originating from the same margin. 此约束是始终确保imageView1textView1从相同的边距开始。

How to define this constraint programmatically ? 如何以编程方式定义此约束?

Rather than using the visual format, you can just instantiate a constraint directly, eg 除了使用视觉格式,您还可以直接实例化约束,例如

[NSLayoutConstraint
    constraintWithItem:imageView1 attribute:NSLayoutAttributeLeading
    relatedBy:NSLayoutRelationEqual
    toItem:textView1 attribute:NSLayoutAttributeLeading
    multiplier:1.0 constant:0.0]

使用Masonry以编程方式设置约束。它非常易于使用,并为用户减少了很多复杂性。

 https://github.com/SnapKit/Masonry

...您可以尝试构建一个UIView ,设置所需的约束,并将其用作UIImageView的占位符( 稍后您可以将其添加到此类视图中 ),或者在另一侧使用UICollectionView

You can set the options argument in constraintsWithVisualFormat:options:metrics:views: check Apple Class Reference . 您可以在constraintsWithVisualFormat:options:metrics:views:设置options参数constraintsWithVisualFormat:options:metrics:views:检查Apple Class Reference

Your code might be as follows 您的代码可能如下

NSString* leadingConstraintsExpression = @"V:[imageView1][textView1]";

NSDictionary* viewsDictionary = NSDictionaryOfVariableBindings(imageView1,textView1);

NSArray* leadingConstraints = [NSLayoutConstraint 
                                  constraintsWithVisualFormat:leadingConstraintsExpression
                                                      options:NSLayoutFormatAlignAllLeading 
                                                      metrics:nil 
                                                        views:viewsDictionary];

[self.view addConstraints:leadingConstraints];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM