当键盘弹出时,设置UIWebView内容不移动

11

我已经进行了足够大的研究,但是找不到答案。

假设我有一个webView,其中有一些文本字段,其中一些放置在屏幕底部,因此当键盘出现时,它应该隐藏那些字段。 在键盘出现后,webView的内容向上滑动,以使那些字段可见。 问题是我不想让内容滑动。

问题是:如何禁用webview的这个功能,或者以某种方式使内容不向上滚动???

谢谢,任何帮助将不胜感激。

5个回答

20

如果您想要禁用所有滚动,包括在表单字段之间导航时的自动滚动,请设置webView.scrollView.scrollEnabled = NO是不够的。这会停止普通的拖动滚动,但无法阻止在网页表单中导航时自动将字段带入视图的滚动。

此外,观察UIKeyboardWillShowNotification可以让您在键盘出现时防止滚动,但如果键盘已经从编辑不同的表单字段上升,则无效。

下面是三个简单步骤来防止所有滚动:

1)创建UIWebView后,禁用普通滚动:

myWebView.scrollView.scrollEnabled = NO;

2) 然后将您的视图控制器注册为scrollView的代理:

myWebView.scrollView.delegate = self;

(并确保在类的@interface定义中添加<UIScrollViewDelegate>,以防止编译器警告)

3) 捕获并撤消所有滚动事件:

// UIScrollViewDelegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    scrollView.bounds = myWebView.bounds;
}

1
感谢提供有趣的信息,但实际上与问题无关。除此之外,我已经从Javascript中解决了它。 - Garnik
2
我发现这确实是你问题的答案。如果可能的话,我更喜欢你发布自己的解决方案。 - Design by Adrian
此解决方案解决了我的问题,我喜欢短语“将字段带入视图滚动”。 - air_bob
谢谢!对我的目的很有效。有一个问题,Richard,你能提供如何将UIScrollViewDelegate添加到@interface的详细信息吗? - Jesse Patel
@Garnik,你用JavaScript有什么解决方案吗?你能发表一下你的答案吗? - Versus
@Versus,非常抱歉,但我已经在7年前发布了这个问题,现在我无法访问那些代码。而且我也不记得我是如何解决那个问题的。此外,由于某种原因,后来我使用了其他解决方案 :( - Garnik

0

我找到了解决方法。当我往上滚动后,我只需要再次向下滚动即可。 首先,我通过添加观察者来捕获通知UIKeyboardWillShowNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 然后实现方法:

-(void)keyboardWillShow:(NSNotification*)aNotification{
NSDictionary* info = [aNotification userInfo];
float kbHeight = [[NSNumber numberWithFloat:[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height]floatValue];
float kbWidth = [[NSNumber numberWithFloat:[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.width]floatValue];
BOOL keyboardIsVisible=[self UIKeyboardIsVisible];
//Check orientation and scroll down webview content by keyboard size
if(kbWidth==[UIScreen mainScreen].bounds.size.width){
    if (!keyboardIsVisible) {
        [[chatView scrollView] setContentOffset:CGPointMake(0, -kbHeight+10) animated:YES];
    } //If is landscape content scroll up is about 113 px so need to scroll down by 113
}else if (kbHeight==[UIScreen mainScreen].bounds.size.height){
    if (!keyboardIsVisible) {
        [[chatView scrollView] setContentOffset:CGPointMake(0, -113) animated:YES];
    }
}
}

这不是我确切要求的,但帮助我解决了问题。

谢谢。


0

Swift5

zoom and scroll hold

class WebviewScrollViewDelegate: NSObject, UIScrollViewDelegate {
    var webview: WKWebView?
    
    func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
        scrollView.pinchGestureRecognizer?.isEnabled = false
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if let webview = webview {
            scrollView.bounds = webview.bounds
            scrollView.zoomScale = webview.pageZoom
        }
    }
}

class MyWebview {
    let webviewScrollViewDelegate = WebviewScrollViewDelegate()

    webview.scrollView.delegate = webviewScrollViewDelegate
    webviewScrollViewDelegate.webview = webview
}

该问题标记为Objective-C,而不是Swift。 - Adrian Mole

0

我认为这个类应该能够解决你的问题。它可以将内容向上滚动,因此如果你的文本框在屏幕底部,它会将其移动到键盘上方。


1
很抱歉,您没有理解我的问题。我希望我的文本字段保持在原地,而不是向上移动。 - Garnik

0

最初我想阻止我的Webview在输入字段(在我的情况下是评论部分)聚焦时缩放。整个视图会自动缩放并使一切混乱。 我使用以下代码解决了这个问题,同时还让Webview停止向上滚动并躲避软键盘:

//Stop the webview from zooming in when the comments section is used.
UIScrollView *scrollView = [_webView.subviews objectAtIndex:0];
scrollView.delegate = self;

//_webView.scrollView.delegate = self; //for versions newer than iOS5.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接