无法在iOS中垂直添加文本字段?

时间:2016-03-19 11:39:21

标签: ios objective-c autolayout nslayoutconstraint

我已经通过故事板垂直添加了一些文本字段。我们想要以编程方式添加更多文本字段但是它总是只添加一个文本字段。 enter image description here

以下是使用的代码

 int i;
    self.main_view.translatesAutoresizingMaskIntoConstraints = NO;
    self.scroll_view.translatesAutoresizingMaskIntoConstraints = NO;

    for (i=0; i<[arr_customEdtTxt count]; i++)
    {
        JVFloatLabeledTextField *tf = [[JVFloatLabeledTextField alloc] initWithFrame:CGRectMake(25, y, 270, 27)];
        tf.textColor = [UIColor blackColor];
        tf.font = [UIFont fontWithName:@"Helvetica" size:14];
        CustomEditText *custom=[arr_customEdtTxt objectAtIndex:i];
        tf.placeholder=custom.field_name;
        tf.textAlignment=NSTextAlignmentLeft;

        previousTextfield=tf;
        //add bottom border
        [self addBorder:tf];
        tf.translatesAutoresizingMaskIntoConstraints = NO;
        [self.main_view addSubview:tf];
        y=y+27;

        [self.main_view addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.main_view  attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0] ];

        [self.main_view addConstraint:[  NSLayoutConstraint constraintWithItem:tf
                                     attribute:NSLayoutAttributeHeight
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:nil
                                     attribute:NSLayoutAttributeHeight
                                    multiplier:1.0
                                      constant:27.0]];
        [self.main_view addConstraint:[  NSLayoutConstraint constraintWithItem:tf
                                                                     attribute:NSLayoutAttributeWidth
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:self.txt_email
                                                                     attribute:NSLayoutAttributeWidth
                                                                    multiplier:1.0
                                                                      constant:0]];
        if(i==0)
        {
            [self.main_view addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.txt_exprience attribute:NSLayoutAttributeBottom multiplier:1 constant:15.0f]];
        }
        else
        {
            [self.main_view addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousTextfield attribute:NSLayoutAttributeBottom multiplier:1 constant:15.0f]];
        }
        [arr_tf addObject:tf];
    }

    [self.main_view layoutIfNeeded];
     [self setDatatoFields];

我遇到约束错误我想在当前文本字段下面添加更多文本字段。请告诉我如何在下面添加越来越多的文本文件

1 个答案:

答案 0 :(得分:0)

看起来previousTextfieldtf引用此行中的相同文字字段:

[self.main_view addConstraint:[NSLayoutConstraint constraintWithItem:tf attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:previousTextfield attribute:NSLayoutAttributeBottom multiplier:1 constant:15.0f]];

另外我注意到,最后您将textField添加到arr_tf,但您还有arr_customEdtTxt,具体取决于您应该使用以下内容更改previousTextfield = tf的textField数组:

if(i > 0){
  //Change arr_tf for arr_customEdtTxt if that is your array
  previousTextfield = [arr_tf objectAtIndex:i-1];
}