2012-02-16 121 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;