UIDatePicker没有在UITable上正确显示

时间:2013-03-09 13:00:25

标签: ios objective-c uitableview uidatepicker

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

Issue

- (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];
}

2 个答案:

答案 0 :(得分:3)

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

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

[preferredDateField setInputView:datePicker];

更合适,符合Apple人机界面指南

答案 1 :(得分:1)

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