UITextField下如何动态添加DatePicker

How to add DatePicker dynamically under UITextField

我不确定它是否能够在点击时在 UITextField 下动态添加 DatePicker。如果可能的话,帮我展示一下示例代码。

在文本字段上设置点击事件

- (void)viewDidLoad {
    [super viewDidLoad];

    [textField addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventEditingDidBegin];
}

设置文本字段版本为假

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return NO;
}

点击文本字段时的方法

 -(IBAction)clicked:(id)sender
    {
        [sender resignFirstResponder];
         picker1   = [[UIDatePicker alloc] initWithFrame:CGRectMake(textField.frame.origin.x-40, textField.frame.origin.y+textField.frame.size.height, textField.frame.size.width, 200)];
        [picker1 setDatePickerMode:UIDatePickerModeDate];
        picker1.backgroundColor = [UIColor whiteColor];
        [picker1 addTarget:self action:@selector(startDateSelected:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:picker1];
    }

当日期值改变时调用此方法

-(IBAction)startDateSelected:(id)sender
{
    NSLog(@"%@",picker1.date);
    [textField setText:[NSString stringWithFormat:@"%@",picker1.date
            ]];
}

希望对您有所帮助! :)