我已获得以下代码:
- (void)messageComposerFrameDidChange:(CGRect)frame withAnimationDuration:(float)duration {
self.webViewBottomConstraint.constant = _keyboardFrame.size.height-15 + frame.size.height;
UIScrollView *scrollView = self.chatConversation.scrollView;
CGRect rect = CGRectMake(0, scrollView.contentSize.height, scrollView.contentSize.width, 1);
[scrollView scrollRectToVisible:rect animated:YES];
NSInteger height = [[_chatConversation stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] intValue];
NSString* javascript = [NSString stringWithFormat:@"window.scrollBy(0, %d);", height];
[_chatConversation stringByEvaluatingJavaScriptFromString:javascript];
}
这段代码完美无缺,唯一的问题是,当我关闭键盘时,它会显示Webview的位置而不是将其调整回正常位置。与textview高度对齐。
WebView属性为_chatConversation
,如图所示
这是日志值
当显示聊天窗口时键盘关闭时:(屏幕1)
2014-09-30 10:57:52.391 mobile-app[737:260398] +-------------------------------------------------+
2014-09-30 10:57:52.392 mobile-app[737:260398] Keyboard Frame: **0.000000**
2014-09-30 10:57:52.392 mobile-app[737:260398] Frame Position Y Position: 512.500000
2014-09-30 10:57:52.393 mobile-app[737:260398] Chat Conversation Height: 401.000000
2014-09-30 10:57:52.393 mobile-app[737:260398] Chat Conversation Y Position: 64.000000
2014-09-30 10:57:52.393 mobile-app[737:260398] +-------------------------------------------------+
当用户点击键入消息时(屏幕2)
2014-09-30 10:58:48.265 mobile-app[737:260398] +-------------------------------------------------+
2014-09-30 10:58:48.266 mobile-app[737:260398] Keyboard Frame: **253.000000**
2014-09-30 10:58:48.266 mobile-app[737:260398] Frame Position Y Position: 259.500000
2014-09-30 10:58:48.267 mobile-app[737:260398] Chat Conversation Height: 463.500000
2014-09-30 10:58:48.267 mobile-app[737:260398] Chat Conversation Y Position: 64.000000
2014-09-30 10:58:48.267 mobile-app[737:260398] +-------------------------------------------------+
当用户点击以关闭键盘时(屏幕3)
2014-09-30 10:59:15.098 mobile-app[737:260398] +-------------------------------------------------+
2014-09-30 10:59:15.098 mobile-app[737:260398] Keyboard Frame: **253.000000**
2014-09-30 10:59:15.098 mobile-app[737:260398] Frame Position Y Position: 512.500000
2014-09-30 10:59:15.099 mobile-app[737:260398] Chat Conversation Height: 210.500000
2014-09-30 10:59:15.099 mobile-app[737:260398] Chat Conversation Y Position: 64.000000
2014-09-30 10:59:15.099 mobile-app[737:260398] +-------------------------------------------------+
如果您看到屏幕3和日志,则键盘框架为253而不是0.0
答案 0 :(得分:0)
如果没有看到更多代码,我的猜测就是你的问题可能出现在这一行:
self.webViewBottomConstraint.constant = _keyboardFrame.size.height-15 + frame.size.height;
messageComposerFrameDidChange
返回的框架也会有origin.y
,因此直接使用该框架可能更容易,而不是手动跟踪_keyboardFrame
。键盘解除后,当代理呼叫进来时,您是否验证了_keyboardFrame.size.height == 0
?
另一种可能性是UIWebView
实际上正确地调整了大小,而内容滚动并没有按预期工作,这导致了屏幕3中的问题。