2

我正在尝试在 iOS 中创建一个包含 3 个标签和 3 个文本字段的视图(普通单视图)。

该应用程序应该能够用于多语言使用。

我想必须遵循以下布局:

Label | TextField
Label | TextField
Label | TextField

TextFields 应该从相同的位置开始。(见图)

但是文本字段的长度应该取决于标签中文本的长度。(我想最小化空间,你可以在英语图像上看到。

简而言之,英文版式的标签和文本字段之间的间距应与德文版相同。

使用自动布局的最佳方法是什么?

图片(英文),到很多空间 图片(德语)

4

1 回答 1

2

每行的第一个:调整标签大小以适合文本。然后在标签和文本字段之间放置一个水平间距。

然后在所有左边缘应排列的文本字段上添加更高优先级的约束。

这是一些针对两行执行的代码:

- (void)viewDidLoad{
      [super viewDidLoad];

      //create labels and text fields for row 1
      UILabel *label0,*label1;
      UITextField *textField0,*textField1;

      label0 = [[UILabel alloc] init];
      label0.backgroundColor=[UIColor redColor];
      [self.view addSubview:label0];


      textField0 = [[UITextField alloc] init];
      textField0.backgroundColor=[UIColor greenColor];
      [self.view addSubview:textField0];

      [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
      [label0 setTranslatesAutoresizingMaskIntoConstraints:NO];
      [textField0 setTranslatesAutoresizingMaskIntoConstraints:NO];

      //*************************************************************
      //layout row 1
      NSArray *arr;
      NSDictionary *dict = NSDictionaryOfVariableBindings(label0,textField0);
      NSLayoutConstraint *constraint;

      //label sized to fit text (default) and standard spacing between label and textField
      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[label0]-[textField0(100)]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];

      //vertical constraints
      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label0]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];

      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[textField0]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];


      //*************************************************************
      //create label and textfield for row2
      label1 = [[UILabel alloc] init];
      label1.backgroundColor=[UIColor redColor];
      [self.view addSubview:label1];

      textField1 = [[UITextField alloc] init];
      textField1.backgroundColor=[UIColor greenColor];
      [self.view addSubview:textField1];

      [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
      [label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
      [textField1 setTranslatesAutoresizingMaskIntoConstraints:NO];

      //*************************************************************
      //layout row 2
      dict = NSDictionaryOfVariableBindings(label0,textField0,label1,textField1);

      //label sized to fit text (default) and standard spacing between label and textField
      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[label1]-[textField1(100)]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];

      //vertical constraints
      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label0]-[label1]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];

      arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[textField0]-[textField1]"
                                                    options:0
                                                    metrics:nil
                                                      views:dict];
      [self.view addConstraints:arr];



      //*************************************************************
      //line up the left edges of the text fields and make it a higher priority to override spacing
      constraint = [NSLayoutConstraint constraintWithItem:textField0 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:textField1 attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
      constraint.priority=UILayoutPriorityDefaultHigh;
      [self.view addConstraint:constraint];

      //*************************************************************
      //Setup some text  
      //label0.text=@"Label0";
      label0.text=@"Some long textasdfasdfadsfasfdasf";
      textField0.text = @"textField0";

      //label1.text=@"Some long text";
      label1.text=@"Label1";
      textField1.text = @"textField1";

      [self.view setNeedsLayout];
}
于 2013-02-13T00:33:58.910 回答