可编辑的webView自动滚动到顶部

gam*_*oul 3 contenteditable uiwebview ipad ios5

我在可编辑的webView中显示了一些文本.只要我向下滚动并触摸某处以编辑渲染文本,它就会滚动到顶部并显示键盘,因此我必须再次向下滚动以进行编辑.有没有办法阻止webView这样做?

Ser*_* N. 6

遇到了同样的问题,仍在寻找这种奇怪行为的正常解决方案.我们仍然无法阻止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];
Run Code Online (Sandbox Code Playgroud)

然后你需要处理键盘通知,如:

- (void)keyboardWillShow:(NSNotification *)aNotification {

// scroll view will scroll to beginning, but we save current offset
[_yourViewWithWebView saveOffset];
    ...
}
Run Code Online (Sandbox Code Playgroud)

之后,您将需要在键盘显示时处理事件:

- (void)keyboardWasShown:(NSNotification*)aNotification{
...
// scroll view scrolled to beginning, but we restore previous offset
[_yourViewWithWebView restoreOffset];
}
Run Code Online (Sandbox Code Playgroud)

因此,在包含UIWebView的视图中,您需要实现:

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];

}
Run Code Online (Sandbox Code Playgroud)

希望一般来说这将帮助您至少部分地解决您的问题.

如果有人会帮助我们阻止每次键盘显示时UIWebView滚动到顶部,我会非常感激.

UIWebView.scrollView.scrollsToTop = NO; 没有帮助.

在显示键盘之前禁用滚动并在显示键盘后启用它也不起作用.

同样在将来,当光标不在UIWebView的可见区域时,您将面临编辑文本的问题 - 并且它不会自动滚动自身以使光标可见.我们已经解决了这个问题,但我正在为我们如何完成这项工作创建详细而可读的教程.如果你已经解决了这个问题,我很感激你看看你的解决方案:)

PS:http://www.cocoanetics.com/2011/01/uiwebview-must-die/

谢谢你,谢尔盖N.