2010-04-06 36 views
0

我有一個視圖,頂部有一個工具欄,在其單元格中有一個帶有文本字段的表格視圖。當我想要編輯位於表格視圖底部的文本字段時,鍵盤隱藏了正在編輯的文本字段(因爲表格視圖沒有向上滾動)。 我試圖實現http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html,但這使得我的整個視圖向上移動(工具欄和表格視圖),同時我想一直在視圖頂部放置toobar。 我修改所提到的代碼是這樣的:如何使用工具欄在視圖頂部滾動表格視圖

- (void)textFieldDidBeginEditing:(UITextField *)textField { 

CGRect textFieldRect = [tableView.window convertRect:textField.bounds fromView:textField]; 
CGRect viewRect = [tableView.window convertRect:tmpTableView.bounds fromView:tableView]; 

CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; 
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height; 
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height; 
CGFloat heightFraction = numerator/denominator; 

if (heightFraction < 0.0) { 
    heightFraction = 0.0; 
} else if (heightFraction > 1.0) { 
    heightFraction = 1.0; 
} 

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 
} else { 
    animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); 
} 

CGRect viewFrame = tableView.frame; 
viewFrame.origin.y -= animatedDistance; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

[tableView setFrame:viewFrame]; 

[UIView commitAnimations]; 


} 

- (void)textFieldDidEndEditing:(UITextField *)textField { 
CGRect viewFrame = tableView.frame; 
viewFrame.origin.y += animatedDistance; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

[tableView setFrame:viewFrame]; 

[UIView commitAnimations]; 
} 

這在頂部做我的工具欄逗留,但不幸的是,表視圖覆蓋的工具欄和工具欄不可見。

任何想法如何解決這個問題?

回答

0

而不是重新定位表視圖,您應該調整它的大小,使鍵盤不重疊。

+0

但我想要滾動表格視圖以在鍵盤上方編輯文本字段。在簡單調整表格視圖的大小的情況下,sam行爲是否可行? – Jakub 2010-04-06 19:34:45

+0

調整大小後,您可以使用'scrollToRowAtIndexPath:atScrollPosition:animated:'滾動任何單元格到可見區域。 – 2010-04-06 20:24:13

+0

如果我可以將任何單元格滾動到可見區域,那麼調整大小的用途是什麼?即使表格視圖是正常大小,我也可以sc right,對吧? btw。我無法制作 scrollToRowAtIndexPath:atScrollPosition:animated:work。我試過了: NSIndexPath * ip = [NSIndexPath indexPathForRow:1 inSection:1]; [self.settingsTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO]; 從第二行滾動第二個單元格(只是爲了查看它是否有效),它隱藏在鍵盤下。 – Jakub 2010-04-07 07:37:34

相關問題