【问题标题】:Prevent UIWebView for Repositioning for input field防止 UIWebView 重新定位输入字段
【发布时间】:2012-01-19 15:50:35
【问题描述】:

我有一个 iPad 应用程序,我在其中将输入表单显示为 UIPopoverController 中的 UIWebView。我的弹出框控制器的大小使得当键盘出现时不必调整它的大小。

当我点击 UIWebView 中的输入字段时,当键盘出现时,Web 视图会被进一步向上推。它被推到文本字段位于框架顶部的位置。 (这是您在移动 Safari 中使用表单时看到的标准行为)。

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

因为我的 webview 的大小已经正确,所以我不想要这种行为。有人对如何防止这种情况有任何建议吗? (目前不可以在这里不使用网络视图)

谢谢!

【问题讨论】:

  • 在我看来你可以用这个做一些 javascript / jquery,所以我会为你编辑你的标签,这样你就可以在那里得到一些答案。
  • 我想知道是否有类似的方法,但忘记标记它:) 谢谢!

标签: javascript jquery ios ipad uiwebview


【解决方案1】:
  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;
    }
    

这对我有用。

【讨论】:

  • 为我工作。谢谢!
【解决方案2】:

我不是 Objective-C 程序员(事实上我根本不了解 Objective-C :-),但我也需要这样做(在我的 Web 应用程序中运行在 iPad 上的 WebView 中),所以在谷歌的“一点”帮助下,我做到了:

- (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];
    });
}

我知道这不是最好的解决方案 - 但它确实有效。从一般编程的角度来看(我不知道 Objective-C),我想可能可以覆盖 UIScrollView 类的 setContentOffset 方法并实现您自己的行为(并可能调用父级的超级方法)。

【讨论】:

    猜你喜欢
    • 2016-01-25
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-25
    • 2013-09-09
    • 1970-01-01
    相关资源
    最近更新 更多