Iphone 在iOS上设计3行对话框的方法-多语言可用

Iphone 在iOS上设计3行对话框的方法-多语言可用,iphone,ios,cocoa-touch,interface-builder,autolayout,Iphone,Ios,Cocoa Touch,Interface Builder,Autolayout,我正在尝试在iOS中创建一个包含3个标签和3个文本字段的视图(普通单视图) 该应用程序应该能够使用多种语言 我希望有以下布局: Label | TextField Label | TextField Label | TextField 文本字段的起始位置应相同。(见图) 但是文本字段的长度应该取决于标签中文本的长度。 (我想尽量减少你在英文图像上看到的空间 简而言之,英文布局的标签和文本字段之间的空间应与德语布局相同 使用自动布局的最佳方法是什么 每行的第一个: 调整标签大小以适合文本。 然

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

该应用程序应该能够使用多种语言

我希望有以下布局:

Label | TextField
Label | TextField
Label | TextField
文本字段的起始位置应相同。(见图)

但是文本字段的长度应该取决于标签中文本的长度。 (我想尽量减少你在英文图像上看到的空间

简而言之,英文布局的标签和文本字段之间的空间应与德语布局相同

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

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

然后在文本字段上添加一个更高优先级的约束,所有左边缘都应该对齐

以下是对两行执行此操作的一些代码:

- (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];
}

您可能想考虑使用模态视图而不是ActVIEW。我想使用一个普通的视图控制器,注意他也可以在XIB中的接口生成器中指定相同的约束……我尝试在接口生成器中设置这个约束。