防止UIWebView重新定位input字段

我有一个iPad应用程序,我在UIPopoverController中显示一个input表单作为UIWebView。 我的popover控制器的大小,使得它不必在键盘出现时resize。

当我点击UIWebView中的input字段时,当出现键盘时,Web视图被进一步推入。 这是推到文本字段在框架的顶部。 (这是在移动Safari浏览器中使用表单时看到的标准行为)。

webview在键盘启动之前根本不能滚动,但是一旦(如第二张图片所示),我可以将webview向下滚动到正确的位置。

因为我的webview已经正确的大小,我不希望这种行为。 有没有人有任何build议如何防止这个? (不使用networking视图目前不是一个选项)

谢谢!

没有键盘的视图查看与键盘

  1. viewDidLoad添加UIKeyboardWillShowNotificationNSNotificationCenter

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
  2. 实现keyboardWillShow:和readjustWebviewScroller方法

     - (void)keyboardWillShow:(NSNotification *)aNotification { [self performSelector:@selector(readjustWebviewScroller) withObject:nil afterDelay:0]; } - (void)readjustWebviewScroller { _webView.scrollView.bounds = _webView.bounds; } 

这对我有用。

我不是一个Objective-C程序员(实际上我根本不知道Objective-C :-),但是我也需要这样做(在WebView中在iPad上运行的Web应用程序中),所以使用Google的“小”帮助我做到了这一点:

 - (void)init { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (void)keyboardWillShow:(NSNotification *)aNotification { float x = self.webView.scrollView.bounds.origin.x; float y = self.webView.scrollView.bounds.origin.y; CGPoint originalOffset = CGPointMake(x, y); for (double p = 0.0; p < 0.1; p += 0.001) { [self setContentOffset:originalOffset withDelay:p]; } } - (void)setContentOffset:(CGPoint)originalOffset withDelay:(double)delay { double delayInSeconds = delay; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [self.webView.scrollView setContentOffset:originalOffset animated:NO]; }); } 

我知道这不是最好的解决scheme – 但它的工作原理。 从一般编程(我不知道Objective-C)的angular度来看,我想可能会覆盖UIScrollView类的setContentOffset方法,并实现您自己的行为(也可能调用父级的超级方法)。