Dynamically insert more UITextFields

前端 未结 5 1575
没有蜡笔的小新
没有蜡笔的小新 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: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.

提交回复
热议问题