Ios UIWebView在键盘出现后自动上移
当键盘出现时,我的UIWebView向上移动,当键盘关闭时,webview不会返回到以前的位置。我已经在键盘出现或关闭前后查看了webview的位置。令人惊讶的是,webview的框架没有改变。但是我之前和之后看到的是完全不同的Ios UIWebView在键盘出现后自动上移,ios,objective-c,uiwebview,xcode5,Ios,Objective C,Uiwebview,Xcode5,当键盘出现时,我的UIWebView向上移动,当键盘关闭时,webview不会返回到以前的位置。我已经在键盘出现或关闭前后查看了webview的位置。令人惊讶的是,webview的框架没有改变。但是我之前和之后看到的是完全不同的 在键盘出现之前: 当键盘出现时,网络视图向上移动 键盘关闭时,网络视图不会下移 到目前为止,我所做的是应用我从其他人那里读到的以下技巧: 1) 观察键盘提示,然后进行适当调整 2) 更改元标记,并在元标记中添加“高度=设备高度” 3) 更改webv
- 在键盘出现之前:
- 当键盘出现时,网络视图向上移动
- 键盘关闭时,网络视图不会下移
注意:我使用iOS7,XCODE 5来管理比屏幕大的内容。调整
UIScrollView
的高度时,通常会影响其滚动位置。使用键盘缩小高度时,会导致内容向上滚动以保持文本字段可见(通过修改contentOffset
),但扩展高度时,只会在底部显示更多内容,而不会将contentOffset
更改回原始值
有很多方法可以解决这个问题,每个创建可编辑文本的人都会在某个时候处理这个问题。这些年来我用了很多,我相信可能还有更好的,我甚至没见过
我上次这样做的方法是修改UIScrollView
的contentInset
,并保存原始contentOffset
,以便以后可以重新填充它
设置您的通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?keyboardRect.size.height:keyboardRect.size.width;
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat animationStyle = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] floatValue];
self.tableViewOffset = self.tableView.contentOffset
UIEdgeInsets contentInsets = self.tableView.contentInset;
contentInsets.bottom = keyboardHeight;
[UIView animateWithDuration:duration delay:0 options:animationStyle animations:^{
self.tableView.contentInset = contentInsets;
} completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat animationStyle = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] floatValue];
UIEdgeInsets contentInsets = self.tableView.contentInset;
contentInsets.bottom = 0;
[UIView animateWithDuration:duration delay:0 options:animationStyle animations:^{
self.tableView.contentInset = contentInsets;
self.tableView.contentOffset = self.tableViewOffset;
} completion:nil];
}
在通知上做一些事情
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGFloat keyboardHeight = UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?keyboardRect.size.height:keyboardRect.size.width;
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat animationStyle = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] floatValue];
self.tableViewOffset = self.tableView.contentOffset
UIEdgeInsets contentInsets = self.tableView.contentInset;
contentInsets.bottom = keyboardHeight;
[UIView animateWithDuration:duration delay:0 options:animationStyle animations:^{
self.tableView.contentInset = contentInsets;
} completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat animationStyle = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] floatValue];
UIEdgeInsets contentInsets = self.tableView.contentInset;
contentInsets.bottom = 0;
[UIView animateWithDuration:duration delay:0 options:animationStyle animations:^{
self.tableView.contentInset = contentInsets;
self.tableView.contentOffset = self.tableViewOffset;
} completion:nil];
}
我解决问题的办法是 1) 观察键盘何时关闭: - (void)observeKeyboard { [NotificationCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)removeObserveKeyboard { [NotificationCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } -(无效)观察板{ [NotificationCenter addObserver:自选择器:@selector(keyboardWillHide:)名称:UIKeyboardWillHideNotification对象:nil]; } -(无效)移除ObserveKeyboard { [NotificationCenter removeObserver:self name:UIKeyboardWillHideNotification对象:nil]; } 2) 当键盘即将隐藏时,我们触发一个js向上滚动它 - (void)keyboardWillHide:(NSNotification *)notification { [self scrollBackToTop]; } //- Fix bug: the UIWebView Content is scroll up when the keyboard appears -(void)scrollBackToTop { [_webView stringByEvaluatingJavaScriptFromString:@"window.scrollTo(0, 0);"]; } -(无效)键盘将隐藏:(NSNotification*)通知{ [自滚动返回到顶部]; } //-修复错误:当键盘出现时,UIWebView内容会向上滚动 -(无效)滚动返回到顶部{ [_WebViewStringByEvaluatingJavaScriptFromString:@“window.scrollTo(0,0);”; }
感谢此链接:您是否使用iScroll或任何其他js框架进行自定义滚动?似乎我们没有使用。但是,在使用自定义滚动的情况下,您的意思是什么?删除css一次,然后尝试生成问题。试一试。tableViewOffset是什么?还有桌面视图?你的意思是webView?
tableViewOffset
只是一个局部变量,我在更改contentOffset
之前用于存储它,以便以后恢复它。网络视图,桌面视图,meh。它们都是UIScrollView
的包装,我们正在讨论如何处理底层的滚动视图。