如何在ios中使用Autolayouts制作水平间距b / w UI字段

时间:2016-02-23 12:29:58

标签: ios objective-c autolayout

在我的应用程序中,我使用Autolayouts在主视图控制器上添加了一个文本字段和一个按钮。

使用以下代码,UItextfild和Button之间不应用水平间距。我怎么可能做错了?

  NSDictionary * views1 = NSDictionaryOfVariableBindings(RoundTripDateTextField,RoundTripButton);

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

1 个答案:

答案 0 :(得分:0)

您的约束中存在一些问题。我已经使用相同的约束编写了一个片段,只是稍微调整了一下:

UIView *RoundTripBackGround = [[UIView alloc] init];
RoundTripBackGround.translatesAutoresizingMaskIntoConstraints = NO;
RoundTripBackGround.backgroundColor = [UIColor yellowColor];
[self.view addSubview:RoundTripBackGround];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[RoundTripBackGround]|"
                                                                  options:0 metrics:nil
                                                                    views:NSDictionaryOfVariableBindings(RoundTripBackGround)]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(20)-[RoundTripBackGround]"
                                                                  options:0 metrics:nil
                                                                    views:NSDictionaryOfVariableBindings(RoundTripBackGround)]];

UITextField *RoundTripDateTextField = [[UITextField alloc] init];
RoundTripDateTextField.translatesAutoresizingMaskIntoConstraints = NO;
RoundTripDateTextField.backgroundColor = [UIColor blueColor];
[RoundTripBackGround addSubview:RoundTripDateTextField];

UIButton *RoundTripButton = [UIButton buttonWithType:UIButtonTypeSystem];
RoundTripButton.translatesAutoresizingMaskIntoConstraints = NO;
RoundTripButton.backgroundColor = [UIColor greenColor];
[RoundTripBackGround addSubview:RoundTripButton];

NSDictionary * views1 = NSDictionaryOfVariableBindings(RoundTripDateTextField,RoundTripButton);

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

[RoundTripBackGround addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[RoundTripDateTextField(30)]|"
                                                                            options:0 metrics:nil
                                                                              views:views1]];

[RoundTripBackGround addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[RoundTripButton(30)]|"
                                                                            options:0 metrics:nil
                                                                              views:views1]];

需要注意的事项:

  1. 使用" | [查看] |"而不是" | -0- [view] -0- |"。
  2. 在最后两个垂直约束中,您没有设置边界约束(" V:| -0- [RoundTripDateTextField(30)]" vs" V:| [RoundTripDateTextField(30)] |&#34)。