首页 文章

键盘关闭时如何调整Webview的大小?

提问于
浏览
1

我有以下代码:

- (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] +-------------------------------------------------+

enter image description here

如果您看到屏幕3和日志,键盘框架为253而不是0.0

1 回答

  • 0

    没有看到更多的代码,我的猜测是你的问题可能在这一行:

    self.webViewBottomConstraint.constant = _keyboardFrame.size.height-15 + frame.size.height;
    

    messageComposerFrameDidChange 返回的帧也将具有 origin.y ,因此直接使用它可能更容易,而不是手动跟踪 _keyboardFrame . 在键盘被解除后代理呼叫进来时,您是否验证了 _keyboardFrame.size.height == 0

    另一种可能性是 UIWebView 实际上正确地调整了大小,而内容滚动没有按预期工作,这导致了屏幕3中的问题 .

相关问题