2016-03-19 3 views
-2

Ich habe einige Textfelder vertikal durch Storyboard hinzugefügt. Unter ihnen möchte ich mehr Textfelder programmatisch hinzufügen, aber es fügt immer nur ein Textfeld hinzu. enter image description hereKann Textfelder vertikal in iOS nicht hinzugefügt werden?

Unten ist der Code

verwendet
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]; 

ich Einschränkungsfehler bekomme ich mehr Textfelder unter dem aktuellen Text fields.Please sagen hinzufügen möchten, wie kann ich mehr und mehr Text fileds unter

hinzufügen

Antwort

0

Es sieht aus wie previousTextfield und tf beziehen sich auf den gleichen Textfeld in dieser Zeile:

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

Auch bemerkte ich, dass Sie am Ende das Textfeld zu arr_tf hinzufügen, aber Sie haben auch arr_customEdtTxt je nach denen eine Array von Textfeldern Sie previousTextfield = tf mit ändern sollte:

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