当动作表被取消时,ios tableview不会向上滚动

时间:2015-02-12 11:15:55

标签: ios uitableview uikeyboard

我正在开发一个聊天应用程序,我使用tableview显示发送和接收的消息以及带有textfield的视图和底部的添加按钮。 单击添加按钮时,将显示带有选项的操作表 一切正常,除非,当我点击添加按钮并点击操作表的取消按钮,然后点击文本字段....当键盘显示时,tableview不向上滚动

enter image description here

以下是keyboardwillshow通知的代码

KeyboardWillShow:

NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey];
    NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];

    CGRect keyboardBounds;
    [keyboardBoundsValue getValue:&keyboardBounds];
    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

    CGRect containerFrame = viewDummy.frame;// viewDummy is the view where keyboard, add button and speak button is displayed
    containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
    NSLayoutConstraint *constPaddingwithViewTable.constant = keyboardBounds.size.height + containerFrame.size.height;

    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];

    // set views with new info
    viewDummy.frame = containerFrame;


    // commit animations
    [UIView commitAnimations];

}

2 个答案:

答案 0 :(得分:0)

之前似乎有一些很好的问题和答案。

以下情况特别好:How to make a UITextField move up when keyboard is present?

这也引用了有关键盘和内容滚动主题的Apple文档。它包含对其示例代码的修复。 Link

答案 1 :(得分:-1)

在下面添加以下设置约束 [self.view layoutIfNeeded];

相关问题