2012-02-16 97 views
1

我在底部有一個滾動視圖,表格視圖和文本框,這將觸發單擊後的鍵盤顯示。 表視圖只是滾動視圖中的一個子視圖,用於顯示該照片的一些註釋。TableView內容高度在鍵盤顯示後更改

開始時,tableView高度顯示正確。但是,在單擊該類中的任何textField之後,tableView高度已更改。任何人都有解決方案。

我已經測試了鍵盤高度。它會影響UITableView的額外高度。 但是我對如何保持鍵盤顯示之前的高度沒有任何意見。

請幫忙。

下面是一些代碼,

//---when a TextField view begins editing--- 
-(BOOL) textFieldDidBeginEditing:(UITextField *)textFieldView { 
     currentTextField = textFieldView; 

     return YES; 
} 

-(BOOL) textFieldShouldReturn:(UITextField *) textFieldView { 
    [textFieldView resignFirstResponder]; 
    return NO; 
} 

//---when a TextField view is done editing--- 
-(void) textFieldDidEndEditing:(UITextField *) textFieldView { 
    currentTextField = nil; 
} 

//---when the keyboard appears--- 
-(void) keyboardDidShow:(NSNotification *) notification { 
    if (keyboardIsShown) return; 

    NSDictionary* info = [notification userInfo]; 

    //---obtain the size of the keyboard--- 
    NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGSize keyboardSize = [aValue CGRectValue].size; 

    //---resize the scroll view (with keyboard)--- 
    CGRect viewFrame = [v_comment_editor frame]; 
    viewFrame.size.height -= keyboardSize.height; 
    v_comment_editor.frame = viewFrame; 

    //---scroll to the current text field--- 
    CGRect textFieldRect = [currentTextField frame]; 
    [v_comment_editor scrollRectToVisible:textFieldRect animated:YES]; 

    keyboardIsShown = YES; 
} 

//---when the keyboard disappears--- 
-(void) keyboardDidHide:(NSNotification *) notification { 
    NSDictionary* info = [notification userInfo]; 

    //---obtain the size of the keyboard--- 
    NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGSize keyboardSize = [aValue CGRectValue].size; 

    //---resize the scroll view back to the original size (without keyboard)--- 
    CGRect viewFrame = [v_comment_editor frame]; 
    viewFrame.size.height += keyboardSize.height; 
    v_comment_editor.frame = viewFrame; 

    keyboardIsShown = NO; 
} 

-(void) viewWillDisappear:(BOOL)animated { 
    //---removes the notifications for keyboard--- 
    [[NSNotificationCenter defaultCenter] 
    removeObserver:self 
    name:UIKeyboardWillShowNotification 
    object:nil]; 
    [[NSNotificationCenter defaultCenter] 
    removeObserver:self 
    name:UIKeyboardWillHideNotification 
    object:nil]; 
} 

回答

0

您顯示的代碼在顯示鍵盤時會調整視圖的大小。它看起來應該在鍵盤被隱藏時恢復到正確的大小。

如果您遇到其中一個子視圖有問題,可能是自動調整大小掩碼以奇怪的方式設置。

圍繞它的最簡單的方法是在你處理類就像一個實例變量:

CGRect tableframe; 

和正確的幀存儲到其在keyboarddidshow功能,並且該表恢復到原來的在** keyboardDidHide **方法中的框架。

0

可能有助於尋找到UIScrollView'scontentOffsetcontentInset性能。這也有助於理解scrollView的boundsframe之間的區別。

我強烈建議創建一個簡單的測試項目並試驗上述概念。徹底的理解會讓你的生活變得更輕鬆。

注意:請注意半透明的導航條及其對上述屬性的影響。

0

對於所有其他人的參考,以防您再次遇到此問題。

我曾經經過這是爲了在keyboardDidHide再次復位的大小,和代碼是如下的溶液:

CGRect frame = tbl_comment.frame; 
    frame.size.height = 145; 
    tbl_comment.frame = frame;