2013-03-09 34 views
2

我有一個UITableCell,並且在那個單元上我有一個UITextBox,當單擊文本框而不是顯示鍵盤時,下面的代碼顯示UIDatePicker。但是我在定位UIDatePicker時遇到了問題。我需要它從頁面的最底部開始,目前它似乎從按下文本框的表格部分的頂部開始。該代碼在用戶單擊首選日期標籤時觸發。任何指針?我附加了一張圖片來顯示它當前正在渲染的位置。謝謝。UIDatePicker沒有顯示在UITable上的正確位置

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

回答

3

我建議你做的是該指令更換您的文本框的inputView(默認爲鍵盤):

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

[preferredDateField setInputView:datePicker]; 

這是更合適,更符合蘋果人機界面指南

+0

謝謝@ Lolloz89虐待這樣做。 – MrBeanzy 2013-03-09 13:36:23

+0

不客氣! – Lolloz89 2013-03-09 15:17:05

1

不要那樣做。創建日期選擇器並將其設置爲文本字段的inputView。然後系統會在適當的位置爲您呈現適當的動畫,而不是鍵盤。

相關問題