我已经进行了足够大的研究,但是找不到答案。
假设我有一个webView
,其中有一些文本字段,其中一些放置在屏幕底部,因此当键盘出现时,它应该隐藏那些字段。 在键盘出现后,webView
的内容向上滑动,以使那些字段可见。 问题是我不想让内容滑动。
问题是:如何禁用webview
的这个功能,或者以某种方式使内容不向上滚动???
谢谢,任何帮助将不胜感激。
我已经进行了足够大的研究,但是找不到答案。
假设我有一个webView
,其中有一些文本字段,其中一些放置在屏幕底部,因此当键盘出现时,它应该隐藏那些字段。 在键盘出现后,webView
的内容向上滑动,以使那些字段可见。 问题是我不想让内容滑动。
问题是:如何禁用webview
的这个功能,或者以某种方式使内容不向上滚动???
谢谢,任何帮助将不胜感激。
如果您想要禁用所有滚动,包括在表单字段之间导航时的自动滚动,请设置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;
}
我找到了解决方法。当我往上滚动后,我只需要再次向下滚动即可。
首先,我通过添加观察者来捕获通知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];
}
}
}
这不是我确切要求的,但帮助我解决了问题。
谢谢。
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
}
我认为这个类应该能够解决你的问题。它可以将内容向上滚动,因此如果你的文本框在屏幕底部,它会将其移动到键盘上方。
最初我想阻止我的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.