设置UIWebView内容在键盘显示时不会移动
我已经做了足够大的研究,但找不到我的问题的答案。设置UIWebView内容在键盘显示时不会移动
假设我有一个webView
其中我有一些文本字段,其中一些放置在屏幕的底部,这样当键盘出现时它应该隐藏这些字段。出现键盘后,webView
的内容向上滑动以使该字段可见。问题是我不要希望内容向上滑动。
问题是:我如何禁用webview
的功能,或以某种方式使内容不能上滚。
谢谢,任何帮助,将不胜感激。
我找到了解决方案。滚动后,我再次将其向下滚动。 首先,我加入观察者赶上通知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];
}
}
}
这不是究竟我问,但帮我解决我的问题。
谢谢。
如果您想禁用所有滚动功能,包括在表单域之间导航时自动滚动,则设置webView.scrollView.scrollEnabled=NO
不能完全覆盖所有内容。这会停止正常的点击并拖动滚动,但不会在浏览Web表单时自动进行视野滚动。
此外,留意UIKeyboardWillShowNotification
会让你防止滚动当键盘出现,但如果键盘是由编辑不同的表单字段已经启动,会做什么。
下面介绍如何防止所有滚动三个简单的步骤:
1)后创建的UIWebView,禁用正常滚动:
myWebView.scrollView.scrollEnabled = NO;
2)然后注册您的视图控制器作为scrollView的代表:
myWebView.scrollView.delegate = self;
(并确保添加<UIScrollViewDelegate>
到类的@interface
定义,以避免编译器警告)
3)捕获并撤消所有滚动事件:
// UIScrollViewDelegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
scrollView.bounds = myWebView.bounds;
}
感谢您提供有趣的信息,但实际上并不涉及这个问题,除此之外,我已经从Javascript解决了这个问题。 – Garnik 2013-09-13 14:11:39
我发现这确实是你的问题的答案。我宁愿如果你发布自己的解决方案。 – 2014-01-14 12:28:47
这个解决方案解决了我的问题,我喜欢短语“带场进入视图滚动” – 2014-08-26 07:56:33
我很抱歉,但你没明白我的问题。我希望我的文本框保持原状,不要向上移动。 – Garnik 2013-04-11 11:49:57