Dynamically insert more UITextFields

前端 未结 5 1573
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 17:34

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

相关标签:
5条回答
  • 2020-12-18 17:47

    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 .

    Hope this will help you.

    All the best!!!

    0 讨论(0)
  • 2020-12-18 17:47

    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...

    0 讨论(0)
  • 2020-12-18 17:54

    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.

    0 讨论(0)
  • 2020-12-18 18:05

    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];
    
    0 讨论(0)
  • 2020-12-18 18:06

    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
    
    0 讨论(0)
提交回复
热议问题