2

我有一个 UITableCell 并且在那个单元格上我有一个 UITextBox,当单击文本框而不是显示键盘时,将显示一个 UIDatePicker,下面的代码。但我无法定位 UIDatePicker。我需要它从页面的最底部开始,此时它似乎从按下文本框的表格部分的顶部开始。当用户单击首选日期标签时,代码会触发。任何指针?我附上了一张图片来显示它当前的渲染位置。谢谢。

问题

- (IBAction)selectDateBegin:(id)sender {


    //Disable Keyboard
    [self.txtBookingDate resignFirstResponder];
    //Disable Scrolling
    [_tableForm setScrollEnabled:false];

    if ([self.view viewWithTag:9]) {
        return;
    }
    CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
    CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);

    UIView *darkView = [[UIView alloc] initWithFrame:self.view.bounds];
    darkView.alpha = 0;
    darkView.backgroundColor = [UIColor blackColor];
    darkView.tag = 9;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDatePicker:)] ;
    [darkView addGestureRecognizer:tapGesture];
    [self.view addSubview:darkView];

    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] ;
    datePicker.tag = 10;
    [datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datePicker];

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
    toolBar.tag = 11;
    toolBar.barStyle = UIBarStyleBlackTranslucent;
    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)];
    [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
    [self.view addSubview:toolBar];

    [UIView beginAnimations:@"MoveIn" context:nil];
    toolBar.frame = toolbarTargetFrame;
    datePicker.frame = datePickerTargetFrame;
    darkView.alpha = 0.5;
    [UIView commitAnimations];
}
4

2 回答 2

3

我建议你做的是用这个指令替换你的 textField 的 inputView (默认是键盘):

UIDatePicker *datePicker = [[UIDatePicker alloc] init] ;
[datePicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];

[preferredDateField setInputView:datePicker];

它更合适并符合 Apple 人机界面指南

于 2013-03-09T13:15:57.203 回答
1

不要那样做。创建一个日期选择器并将其设置为inputView您的文本字段。然后系统将在正确的位置以正确的动画而不是键盘为您呈现它。

于 2013-03-09T13:15:31.683 回答