Ramu Pasupuleti
Ramu Pasupuleti

Reputation: 886

Dynamically insert more UITextFields

I have a requirement as described in the attached screen shot. When blue add button is clicked, one more UItextfield have to be inserted between last text field and textview and Blue add button will have to appear beside that dynamically inserted new uitextfield. Could any one suggest how to achieve this. I have placed all the fields in UIScrollview.

enter image description here

Scroll view is not enabled:

enter image description here

Upvotes: 0

Views: 574

Answers (5)

Yashesh
Yashesh

Reputation: 1752

First get the frame of the UItextField. Now increase yPos of the previous textFeild and at UITetField new position. And to re arrange UITextField which are below that textField just increase yPos of each TextField.

Upvotes: 0

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

You can do as :

In .h I have an outlet called previousTextField which is hooked to the first one. As name suggests it will store the latest added textField.

Find running project here.

-(IBAction)addTextField:(id)sender{
    float x=previousTextField.frame.origin.x;
    float y=previousTextField.frame.origin.y+50;//get y from previous textField and add 10 or so in it.
    float w=previousTextField.frame.size.width;
    float h=previousTextField.frame.size.height;
    CGRect frame=CGRectMake(x,y,w,h);
    UITextField *textField=[[UITextField alloc] initWithFrame:frame];
    textField.placeholder = @"Enter User Name or Email";


    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.font = [UIFont systemFontOfSize:15];
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;


    [self.view addSubview:textField];
    previousTextField=textField;
}

Just an idea/algorithm how to go with, not compiler checked

I missed on thing, changing the location of + button. I think you can do it :)

EDIT: add following in above method

//move addbutton which is an outlet to the button
CGRect frameButton=CGRectMake(addButton.frame.origin.x, addButton.frame.origin.y+50, addButton.frame.size.width, addButton.frame.size.height);
[addButton removeFromSuperview];
[addButton setFrame:frameButton];
[self.view addSubview:addButton];

Upvotes: 4

Kalyani
Kalyani

Reputation: 392

see output of my code.. enter image description here

You can use following code...i have done it & it is running as per your screen shot.. in your .h file just declare this variable

      int newY,newY1;
      UIButton *btn;

then your .m file in viewDidLoad method add following code

   UITextField *txt=[[UITextField alloc]init];
   txt.frame=CGRectMake(50,30,140,30);
   txt.placeholder = @"Enter User Name or Email";
   txt.borderStyle = UITextBorderStyleRoundedRect;

   [self.view addSubview:txt];
   newY=txt.frame.origin.y;
   btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
   btn.frame=CGRectMake(250,30, 30, 30);
   [btn addTarget:self action:@selector(addtxtField) forControlEvents:UIControlEventTouchUpInside];
   [self.view addSubview:btn];

& then create one addtxtField method..see below

 -(void)addtxtField
{
newY=newY+50;

UITextField *nwtxt=[[UITextField alloc]init];
nwtxt.frame=CGRectMake(50,newY,140,30);
nwtxt.placeholder = @"Enter User Name or Email";
nwtxt.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:nwtxt];

btn.frame=CGRectMake(250,newY, 30, 30);
[self.view addSubview:btn];

}

it is 100% running as per your requiement...

Upvotes: 0

Manann Sseth
Manann Sseth

Reputation: 2745

Output

Try this ::

In .h file

@property(nonatomic, retain) IBOutlet UIScrollView *scr;
@property(nonatomic, retain) IBOutlet UITextField *txt;
@property(nonatomic, retain) IBOutlet UITextView *txtVw;
@property(nonatomic, retain) IBOutlet UIButton *btn;

-(IBAction)click:(id)sender;

In .m file

int cnt;

float x = 0.0, y = 0.0, w = 0.0, h = 0.0;

- (void)viewDidLoad
{
    [super viewDidLoad];

    scr.delegate = self;
    scr.contentSize = CGSizeMake(0, 400);

    cnt = 0;
}

-(IBAction)click:(id)sender
{
    if (cnt == 0) {
       x=txt.frame.origin.x;
       y=txt.frame.origin.y + 50;//get y from previous textField and add 10 or so in it.
       w=txt.frame.size.width;
       h=txt.frame.size.height;
    }
    else
    {
       y+=50;
    }

    UITextField *textField=[[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"Enter User Name or Email";

    [scr addSubview:textField];

    [btn setFrame:CGRectMake(248, y, 30, 30)];

    float y1 = y + 100;
    [txtVw setFrame:CGRectMake(orign_x, y1, origin_w, origin_h)];

    cnt++;
}

Hope, it'll definately help you.

Thanks.

Upvotes: 0

Dhara
Dhara

Reputation: 4089

On add button add a textfeild in subview and give the textfeild a unique tag so it will help you to differentiate between all textfeilds. For setting frame take the reference of your last textfeild frame and accordingly set the y coordinate.

UITextField *textField = [[UITextField alloc] initWithFrame:yourFrame];
textField.delegate = self;
textField.tag=yourtag;
[scrollview addSubview:textField];
[textField release];//if arc is disable

Upvotes: 0

Related Questions