dynamic插入更多的UITextFields

我有一个要求,如所附的屏幕截图所述。 当单击蓝色添加button时,必须在最后一个文本字段和文本UItextfield之间插入另外一个UItextfield ,并且蓝色添加button将不得不出现在dynamic插入的新的uitextfield旁边。 任何人都可以build议如何实现这一点。 我已经把所有的字段放在UIScrollview

在这里输入图像说明

滚动视图未启用:

在这里输入图像说明

你可以这样做:

在.h我有一个叫做previousTextField的sockets,它被第一个连接了。 顾名思义,它将存储最新添加的textField。

find正在运行的项目 。

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

只是一个想法/algorithm如何去,而不是编译器检查

我错过了,改变了+button的位置。 我认为你可以做到这一点:)

编辑:在上面的方法中添加以下内容

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

在添加button添加在子视图textfeild,并给textfeild一个独特的标签,所以它会帮助你区分所有textfeild。 为了设置框架,请参考上一个文本框架框架并相应地设置y坐标。

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

首先获取UItextField的框架。 现在增加以前的textFeild的yPos和在UITetField新的位置。 而重新安排在textField下面的UITextField只是增加每个TextField的yPos。

希望这会帮助你。

祝一切顺利!!!

产量

尝试这个 ::

在.h文件中

 @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; 

在.m文件中

 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++; } 

希望,它一定会帮助你。

谢谢。

看到我的代码的输出 在这里输入图像说明

你可以使用下面的代码…我已经完成了它并按照你的屏幕截图运行..在你的.h文件中,只需声明这个variables

  int newY,newY1; UIButton *btn; 

那么你的.m文件在viewDidLoad方法中添加下面的代码

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

然后创build一个addtxtField方法..见下面

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

}

它按照您的要求是100%运行…