0

In my app I want to programmatically add textfield below another if required on click of a button. I already had provided two textFields. if a user want to add another textfield he can do so by clicking a button. I have already written code to obtain the textfield but the problem is that it overlaps the already designed textFields. How can I do it?

Is there any way through which I can get the x and Y co-ordinates of already designed textfield so that I can place new textField relative to those co-ordinates.

1
  • First of all, post your code, let us SEE the problem you're talking about. Second, yes it is possible, please google the UIView property frame. Third, you should learn autoLayout instead, its tougher but worth it in the long run Commented Apr 2, 2015 at 12:47

2 Answers 2

1

This code add textField to view dynamically when every click action on button

ExampleViewController.h

#import <UIKit/UIKit.h>

@interface ExampleViewController :UIViewController<UITextFieldDelegate>

@property int positionY;
@property int fieldCount;

@property (strong,nonatomic)  UIScrollView *scroll;

@end

ExampleViewController.m

#import "ExampleViewController.h"

@interface ExampleViewController ()

@end

@implementation ExampleViewController

@synthesize positionY;
@synthesize fieldCount;
@synthesize scroll;

- (void)viewDidLoad {
  [super viewDidLoad];



scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.backgroundColor = [UIColor whiteColor];

[self.view addSubview:scroll];

UIButton *clickToCreateTextField = [[UIButton alloc] initWithFrame:CGRectMake(40, 80, self.view.frame.size.width-80, 75)];
[clickToCreateTextField setTitle:@"Create Text Field" forState:UIControlStateNormal];
[clickToCreateTextField addTarget:self action:@selector(clickedButton) forControlEvents:UIControlEventTouchUpInside];
[clickToCreateTextField setBackgroundColor:[UIColor blackColor]];
[scroll addSubview:clickToCreateTextField];

positionY = clickToCreateTextField.center.y;
fieldCount = 0;


// Do any additional setup after loading the view.
}

-(void) clickedButton{
  //add text field programmitacally
  UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(40, positionY, self.view.frame.size.width-80, 75)];
  textField.delegate = self;
//give a tag to determine the which textField tapped
textField.tag = fieldCount;
textField.placeholder = [NSString stringWithFormat:@"Your dynamically created textField: %d", fieldCount ];
[scroll addSubview:textField];

//check if the textFields bigger than view size set scroll size and offset
if (positionY>= self.view.frame.size.height) {
    scroll.contentOffset = CGPointMake(0, positionY);
    scroll.contentSize = CGSizeMake(scroll.frame.size.width, scroll.frame.size.height+positionY);
}

fieldCount++;
//increase the position with a blank place
positionY = positionY+textField.frame.size.height+20;
}
#pragma mark TextField Delegate Methods
  -(void) textFieldDidBeginEditing:(UITextField *)textField{
//Do what ever you want
}

-(void) textFieldDidEndEditing:(UITextField *)textField{
  [textField resignFirstResponder];
//do anything
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
  [textField resignFirstResponder];
  return YES;
}

-(void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

You can do any additional changes on this code. I think this example explain your answer.

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Use a counter and calculate y like this counter*texfield.frame.size.height.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.