我们如何使用自动布局以编程方式在两个视图之间设置水平空间(使用constraintWithItem model formate)

时间:2015-07-27 07:37:16

标签: ios objective-c

嗨我是ios的初学者在我的应用程序中我希望以编程方式使用自动布局(constraintWithItem模型)添加两个UIview但是horizental空间不设置b / w这两个视图根据我的代码屏幕就像第一个屏幕一样

我的代码在

之下
 //Applying autolayouts for myview1
        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myview1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f]];

        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myview1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]];

        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myview1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f]];

        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:myview1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f]];

       // Applying autolayouts for myview2

        NSDictionary * views = NSDictionaryOfVariableBindings(myview2,myview3);


        [myview1 addConstraint:[NSLayoutConstraint constraintWithItem:myview2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:50.0f]];

        [myview1 addConstraint:[NSLayoutConstraint constraintWithItem:myview2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:myview1 attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f]];

   [myview1 addConstraint:[NSLayoutConstraint constraintWithItem:myview2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myview1 attribute:NSLayoutAttributeTop multiplier:1.0f constant:80.0f]];


        NSArray * horizontalConstraintsforbuttons = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[myview2]-10-[myview3]-|" options:0 metrics:nil views:views];

        [myview1 addConstraints:horizontalConstraintsforbuttons];

       // Applying autolayouts for myview3

        [myview1 addConstraint:[NSLayoutConstraint constraintWithItem:myview3 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:50.0f]];

        [myview1 addConstraint:[NSLayoutConstraint constraintWithItem:myview3 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:myview1 attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f]];

        [myview1 addConstraint:[NSLayoutConstraint constraintWithItem:myview3 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:myview1 attribute:NSLayoutAttributeTop multiplier:1.0f constant:80.0f]];

enter image description here

但我希望获得第二个屏幕的结果,请帮我一些

enter image description here

1 个答案:

答案 0 :(得分:0)

这里有2个错误:

1)您要添加2个前导约束来查看2和2个尾随约束来查看3.一个来自一行代码(“NSLayoutAttributeLeading”等),第二个来自可视化格式

2)你没有以任何方式告诉自动布局如何设置myview2和myview3的宽度。因此,一个视图获得所有可用宽度,另一个视图获得另一个视图。

您可以通过更改

来解决这两个问题
    NSArray * horizontalConstraintsforbuttons = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[myview2]-10-[myview3]-|" options:0 metrics:nil views:views];

NSArray * horizontalConstraintsforbuttons = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[myview2(==myview3)]-10-[myview3]" options:0 metrics:nil views:views];

这消除了额外的前导和尾随约束,并告诉自动布局myview2和myview3的宽度应相等。