如何在Ios中使用自动布局制作垂直间距B / W UIField

时间:2016-02-24 09:59:06

标签: ios objective-c autolayout

enter image description here

您好我是iOS的新手,在我的应用中我使用自动布局视觉格式

但是如何在各种条件下改变垂直间距

为此,我使用下面的代码,但垂直间距没有设置b / w UIfields

看到我在这里给出的屏幕垂直空间“15”,但它没有应用

我的代码: -

- (void)viewDidLoad {
    [super viewDidLoad];

 NSDictionary * views1 = NSDictionaryOfVariableBindings(RoundTripLabel,RoundTripDateTextField,RoundTripButton,BottomLabel1,BottomLabel2);


//Appying Horizental contstraints:-

    [MainScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[RoundTripLabel]-8-|" options:0 metrics:nil views:views1]];

    [MainScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[RoundTripDateTextField]-8-[RoundTripButton(30)]-|" options:0 metrics:nil views:views1]];

    [MainScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[BottomLabel1]-8-|" options:0 metrics:nil views:views1]];

    [MainScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[BottomLabel2]-8-|" options:0 metrics:nil views:views1]];


    //Appying Vertical contstraints:-

    [MainScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[RoundTripLabel(17)]-10-[RoundTripDateTextField]-0-[BottomLabel1(17)]-10-[BottomLabel2(17)]" options:0 metrics:nil views:views1]];

     NSDictionary * views2 = NSDictionaryOfVariableBindings(RoundTripLabel,RoundTripButton);

    [MainScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[RoundTripLabel]-10-[RoundTripButton]" options:0 metrics:nil views:views2]];


    //Defining  Heights Using Constraint With Item Formate:-

    RoundTripDateTextFieldHeight = [NSLayoutConstraint constraintWithItem:RoundTripDateTextField attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
    [MainScrollView addConstraint:RoundTripDateTextFieldHeight];

    RoundTripButtonHeight = [NSLayoutConstraint constraintWithItem:RoundTripButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:30.0f];
    [MainScrollView addConstraint:RoundTripButtonHeight];

    VerticalSpacing = [NSLayoutConstraint constraintWithItem:BottomLabel1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:RoundTripDateTextField attribute:NSLayoutAttributeBottom multiplier:1.0f constant:15.0f];
    [MainScrollView addConstraint:RoundTripDateTextFieldHeight];

    [MainScrollView setNeedsDisplay];

}

1 个答案:

答案 0 :(得分:1)

在您的代码中,这些约束存在问题

第一个同意:

而不是再次添加RoundTripDateTextFieldHeight,您需要添加VerticalSpacing

VerticalSpacing = [NSLayoutConstraint constraintWithItem:BottomLabel1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:RoundTripDateTextField attribute:NSLayoutAttributeBottom multiplier:1.0f constant:15.0f];
[MainScrollView addConstraint:VerticalSpacing];  // instead of [MainScrollView addConstraint:RoundTripDateTextFieldHeight];

第二次

在2个标签之间添加垂直间距

NSLayoutConstraint *VerticalSpacingLbl2 = [NSLayoutConstraint constraintWithItem:BottomLabel2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:BottomLabel1 attribute:NSLayoutAttributeBottom multiplier:1.0f constant:10.0f];
[MainScrollView addConstraint:VerticalSpacingLbl2];  

第3次

更改以下约束

  //Appying Vertical contstraints:-

  [MainScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[RoundTripLabel(17)]-10-[RoundTripDateTextField]-0-[BottomLabel1(17)]-10-[BottomLabel2(17)]" options:0 metrics:nil views:views1]];

用这个

 [MainScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-100-[RoundTripLabel(17)]-10-[RoundTripDateTextField]" options:0 metrics:nil views:views1]];