ios 动态插入更多UITextFields

qgzx9mmu  于 2023-01-10  发布在  iOS
关注(0)|答案(5)|浏览(112)

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

滚动视图未启用:

h9a6wy2h

h9a6wy2h1#

您可以执行以下操作:
在.h中,我有一个名为previousTextField的outlet,它与第一个outlet挂钩,顾名思义,它将存储最新添加的textField。
查找正在运行的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;
}
  • 只是一个想法/算法如何去,而不是编译器检查 *

我错过了一件事,改变了+按钮的位置。我想你可以做到:)

EDIT:在上述方法中添加以下内容

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

e37o9pze2#

点击添加按钮,在子视图中添加一个文本字段,并给予文本字段一个唯一的标签,这样可以帮助你区分所有的文本字段。对于设置框架,参考你最后一个文本字段框架,并相应地设置y坐标。

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

cgh8pdjw3#

试试这个:
在. 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++;
}

霍普,这绝对能帮到你.
谢谢。

hgqdbh6s

hgqdbh6s4#

查看我的代码的输出..

你可以使用下面的代码...我已经做了它&它正在运行根据您的屏幕截图..在您的.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方法(& T)..请参见下文

-(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%按照你的要求运行的。

klr1opcd

klr1opcd5#

首先获取UItextField的帧。现在增加前一个textField的yPos,并在UITetField的新位置。要重新排列该textField下面的UITextField,只需增加每个textField的yPos。

相关问题