动态插入更多UITextFields(Dynamically insert more UITextF

2019-07-22 06:05发布

我如所附截屏中描述的要求。 当点击蓝色的添加按钮,多一个UItextfield必须最后插入文本字段的TextView和蓝色之间添加按钮,将出现动态插入新的UITextField旁边。 可以在任何一个建议如何实现这一目标。 我已经摆在所有领域UIScrollview

滚动视图未启用:

Answer 1:

你可以做:

在.H我有一个名为出口previousTextField它钩住第一个。 正如名字所暗示它将存储最新添加文本框。

查找运行此项目 。

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

只是一个想法/算法如何去用,没有编译器检查

我错过的东西,改变+按钮的位置。 我认为你可以做到这一点:)

编辑:添加在上述方法中以下

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


Answer 2:

添加按钮在子视图中添加textfeild并给予textfeild一个独特的标记,以便它会帮助你所有textfeilds区分。 对于设置框把你的最后textfeild帧的参考,并设置相应的y坐标。

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


Answer 3:

首先得到的UITextField的框架。 现在,在增加新的UITetField位置以前textFeild和yPos。 并重新安排的UITextField它是文本框下面只是增加每个文本字段的yPos。

希望这会帮助你。

祝一切顺利!!!



Answer 4:

试试这个 ::

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

希望,这肯定会帮助你。

谢谢。



Answer 5:

看到我的代码输出..

您可以使用下面的代码...我已经做到了与正在运行,按您的屏幕截图..在.h文件中只是声明这个变量

      int newY,newY1;
      UIButton *btn;

然后在viewDidLoad方法您的m文件中添加以下代码

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

&然后创建低于一个addtxtField method..see

 -(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%运行,按您的requiement ...



文章来源: Dynamically insert more UITextFields