- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在可编辑的 web View 中显示了一些文本。一旦我向下滚动并触摸某处以编辑呈现的文本,它就会滚动到顶部并出现键盘,因此我必须再次向下滚动以进行编辑。有没有办法阻止 webView 这样做?
最佳答案
遇到同样的问题,仍在寻找这种奇怪行为的正常解决方案。我们仍然无法阻止 UIWebView 这样做,如果你在 iPad 上查看 Evernote 应用程序,你会在那里看到同样的问题,不幸的是:(我们唯一能做的就是在显示键盘时保存 UIWebView 的 contentOffset 并在打开键盘后恢复。
这看起来像:
//register your controller for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) UIKeyboardDidShowNotification object:nil];
然后您将需要处理键盘通知,例如:
- (void)keyboardWillShow:(NSNotification *)aNotification {
// scroll view will scroll to beginning, but we save current offset
[_yourViewWithWebView saveOffset];
...
}
之后,您需要在显示键盘时处理事件:
- (void)keyboardWasShown:(NSNotification*)aNotification{
...
// scroll view scrolled to beginning, but we restore previous offset
[_yourViewWithWebView restoreOffset];
}
因此,在您需要实现的包含 UIWebView 的 View 中:
static CGPoint editableWebViewOffsetPoint;
- (void) saveOffset{
editableWebViewOffsetPoint = yourWebView.scrollView.contentOffset;
}
- (void) restoreOffset{
//just use animation block to have scroll animated after jumping to top and back to old position
[UIView animateWithDuration:.2
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
yourWebView.scrollView.contentOffset = editableWebViewOffsetPoint;
}
completion:nil];
}
总体而言,希望这将帮助您至少部分解决您的问题。
如果有人能帮助我们防止每次显示键盘时 UIWebView 滚动到顶部,我将不胜感激。
UIWebView.scrollView.scrollsToTop = NO;
没有帮助。
在显示键盘之前禁用滚动并在显示键盘后启用它也不起作用。
此外,当光标不在 UIWebView 的可见区域时,您将面临编辑文本的问题 - 它不会自动滚动以使光标可见。我们已经解决了这个问题,但我正在创建详细且易读的教程,说明我们如何做到这一点。如果您已经解决了这个问题,我会很高兴看到您的解决方案:)
PS:http://www.cocoanetics.com/2011/01/uiwebview-must-die/
谢谢你,谢尔盖 N.
关于ipad - 可编辑的 webView 自行滚动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11520184/
我发现以下帖子非常有帮助: How to pickle yourself? 但是,此解决方案的局限性在于,重新加载类时,它不会以其“运行时”状态返回。即它将重新加载所有变量等以及类在转储时的一般状态.
我是一名优秀的程序员,十分优秀!