2011-06-11 20 views
0

我有一個比窗口短的UITableView,因此它不需要滾動。然而,它足夠長,當選擇底行中的文本字段時,鍵盤覆蓋它。我有一個UITextField在被選中時被鍵盤隱藏。它怎麼能被看到?

我不能使用scrollToRowAtIndexPath,因爲表格比窗口更短,所以我想知道將其引入視圖的正確方法是什麼。

我在想滑動的全貌了像素的定數,雖然這似乎很不禮貌的,因爲它會破壞UI如果我增加了更多的行表。

回答

2

你應該實現在關注類以下方法:

- (void) textFieldDidBeginEditing:(UITextField *)myTextField 
{ 
    [self animateTextField:myTextField up:YES]; 
} 

- (void) textFieldDidEndEditing:(UITextField *)myTextField 
{ 
    [self animateTextField:myTextField up:NO]; 
} 

- (void) animateTextField: (UITextField*) textField up: (BOOL) up 
{ 
    int movement = (up ? -105 : 105); 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:0.3f]; 
    self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
    [UIView commitAnimations]; 
} 

你得值(-1051050.3f)適應您的情況。

1

你可以擁有整個的tableView通過設置footerView的高度向上滑動。鍵盤將移動上面的頁腳高度

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section 
{ 
return 70.0; 
} 
+0

看起來像一個很好的方式來做到這一點,但我不知道如何實現它。是否像在我創建表的類的實現文件中聲明方法一樣簡單? 我在哪裏傳入部分參數? – espekia 2011-06-11 22:36:56

+0

只需將此添加到您要更改的tableView的.m文件:) – Legolas 2011-06-11 23:12:00

0

Pierre的代碼爲我工作,但我刪除了myTextField參數。這似乎沒有必要。我也將這改爲UITextViews,因爲這就是我的代碼。否則,感謝您的問題和答案。我一直在靠牆敲打我的頭來解決這個問題!

#pragma mark - Text View Delegate 

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    [self animateTextViewUp:YES]; 
} 

- (void) textViewDidEndEditing:(UITextView *)textView 
{ 
    [self animateTextViewUp:NO]; 
} 

- (void) animateTextViewUp:(BOOL)up 
{ 
    int movement = (up ? -80 :80); 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:0.3f]; 
    self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
    [UIView commitAnimations]; 
} 
相關問題