设置UIWebView内容时不显示键盘时移动

Gar*_*nik 11 keyboard focus objective-c uiwebview uitextfield

我做了足够多的研究,但找不到我的问题的答案.

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

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

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

Ric*_*her 18

如果要禁用所有滚动,包括在表单域之间导航时自动滚动,则设置webView.scrollView.scrollEnabled=NO并不能完全覆盖所有内容.这会停止正常的点击并拖动滚动,但不会在您在Web表单中导航时自动进行场景拖动滚动.

此外,观看UIKeyboardWillShowNotification时会让您在键盘出现时阻止滚动,但如果键盘已经编辑不同的表单字段,则无法执行任何操作.

以下是如何通过三个简单步骤来防止所有滚动:

1)创建UIWebView后,禁用正常滚动:

myWebView.scrollView.scrollEnabled = NO;
Run Code Online (Sandbox Code Playgroud)

2)然后将视图控制器注册为scrollView的委托:

myWebView.scrollView.delegate = self;
Run Code Online (Sandbox Code Playgroud)

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

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

// UIScrollViewDelegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    scrollView.bounds = myWebView.bounds;
}
Run Code Online (Sandbox Code Playgroud)

  • 我发现这确实是你问题的答案.如果你发布自己的解决方案我会更喜欢. (2认同)

Gar*_*nik 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];
    }
}
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢。