2009-12-03 33 views
0

我試圖在表格視圖底部的表格單元格中實現UITextView。編輯視圖框架和原點後,UITableView不會滾動

我已經嘗試了這裏的建議Making a UITableView scroll when text field is selected以及其他解決方案,但它們有點不同,因爲我必須人爲地爲當前視圖添加額外的高度以便爲鍵盤創建空間。

下面是我添加到以前的解決方案,以便將其移植到我的應用程序。

-(void) keyboardWillShow:(NSNotification *)note { 
     CGRect frame = self.view.frame; 
     frame.size.height += keyboardHeight; 
     frame.origin.y -= keyboardHeight; 
     self.view.frame = frame; 
} 

-(void) keyboardWillHide:(NSNotification *)note 
{ 
     CGRect frame = self.view.frame; 
     frame.size.height -= keyboardHeight; 
    frame.origin.y += keyboardHeight; 

} 

這樣做將正確的高度添加到視圖並滾動到細胞,但恢復原始視圖的高度,滾動超越目前可見視圖變得不可能,即使有邊界之外有效的內容後, (我看到滾動條反彈之前的文本視圖)。
如果我嘗試在keyboardWillShow中保存tableview的框架或邊界(而不是視圖)並在keyboardWillHide中恢復它們,滾動將被恢復,但視圖將被減半。

除了硬編碼視圖底部的附加高度之外,是否還有任何補救措施?

回答

3

我能夠通過刪除編輯視圖原點的代碼來解決鎖定滾動問題。另外,我通過在我的計算中使用tableview的contentSize屬性來實現滾動到底部單元格。

-(void) keyboardWillShow:(NSNotification *)note 
{ 

    if(!isKeyboardShowing) 
    { 
    isKeyboardShowing = YES; 
    CGRect keyboardBounds; 
    [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &keyboardBounds]; 
    CGFloat keyboardHeight = keyboardBounds.size.height; 

      CGRect frame = self.view.frame; 
      frame.size.height += keyboardHeight; 
      self.view.frame = frame; 

    CGPoint scrollPoint = frame.origin; 
    scrollPoint.y += _tableView.contentSize.height - keyboardHeight; 
    [_tableView setContentOffset:scrollPoint animated:YES]; 
    } 
} 
相關問題