UIWebView仅在输入焦点iOS 6上向下滚动 [英] UIWebView scrolling down on input focus iOS 6 only

查看:79
本文介绍了UIWebView仅在输入焦点iOS 6上向下滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以模态加载UIWebView。该网页有一些输入完全按照预期在iOS 5中工作。但是在iOS 6中,只要输入获得焦点,键盘就会自动对齐表单,即使输入有足够的空间显示在上面键盘。由于某些输入位于页面顶部,因此会强制它们脱离视图,因此用户无法看到他们正在键入的内容。向上滚动以查看输入会导致键盘停止工作,直到被解除并重新聚焦输入(然后再将其滚动到视图外)。这是iOS 6中的预期行为吗?当输入获得焦点时(如iOS 5如何工作),有没有办法阻止webview滚动?

I'm loading a UIWebView in a modal. The web page has some inputs that work exactly as intended in iOS 5. However in iOS 6, anytime an input gets focus, the keyboard is doing it's automatic 'centering' of the form even though the inputs have plenty of room to be shown above the keyboard. Since some of the inputs are at the top of the page, this forces them out of view so the user cannot see what they are typing. Scrolling back up to see the inputs causes the keyboard to stop working until dismissed and refocusing the input (in turn scrolling it out of view again). Is this an expected behavior in iOS 6? Is there any way to keep the webview from scrolling when an input gains focus (like how iOS 5 works)?

推荐答案

不知道你的代码的内部工作原理,我会尽力帮助你。如果不出意外,我希望能提供一些思考的食物。你问了两个问题:

Without knowing the inner workings of your code, I'm going to try to help as much as I can. If nothing else, I hope to provide some food for thought. You asked two questions:

这是iOS6中的预期行为吗?

什么你看到的确很奇怪。奇怪的是,webView使表单居中,而不是以输入字段为中心。它绝对不应该导致活动输入字段滚出视图。此外,键盘似乎停止工作,这是非常奇怪的。但是,对于webView滚动,预计会在iOS 5和6中看到不同的行为。如你所说,iOS 5将inputField滚动到视图中,而iOS6将其置于中心位置。

What you're seeing is quite odd for sure. It's strange that the webView is centering the form rather than centering the input field. And it should definitely not cause the active input field to scroll out of view. Furthermore, the keyboard seems to stop working, which is quite odd. However, it is expected to see different behavior in iOS 5 and 6 with regards to the webView scrolling. As you said, iOS 5 scrolls the inputField into view while iOS6 puts it in center.

当输入获得焦点时(如iOS 5如何工作),有没有办法阻止webview滚动?

是的。我提供了下面的代码 - 即阻止webView滚动。如果这正是你想要做的,那就太好了。但是,停止滚动webView与获取与iOS5相同的行为不同。不过,我还是想按你的要求给你这个选项。

Yes. I provide code to do just this below -- namely to stop the webView from scrolling. If that is exactly what you want to do, then great. However, stopping the webView from scrolling is not the same as getting the same behavior as iOS5. Still, I wanted to give you this option as you requested it.

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate> {
    CGPoint origin;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
    [self.webView loadRequest:request];
    self.webView.scrollView.delegate = self;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    // NOTE THAT SIMPLY DISABLING THE SCROLL WILL NOT PREVENT KEYBOARD SCROLL
    //[self.webView.scrollView setScrollEnabled:NO];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)keyboardWillShow {
    NSLog(@"keyboard");
    origin = self.webView.scrollView.contentOffset;

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"scroll");
    [self.webView.scrollView setContentOffset:origin];
}

@end

如果你想要一致的滚动行为......

我花了一些时间来编写这个滚动修改代码。您可以使用它作为参考,使您自己的滚动行为在iOS 5和6中保持一致。在此代码中:当用户单击网页上的文本输入框时,代码将通过保持页面滚动到其当前位置来停止默认滚动行为。不幸的是,没有办法取消所有滚动,而是你必须覆盖滚动。一旦默认滚动被抑制,它就会获得活动输入框的位置并滚动到该位置。这会将活动输入框置于屏幕顶部。您可以根据自己的喜好进行修改。例如,您可以使用[UIScrollView scrollRectToVisible]使其更像iOS5。

I took some time to code up this scroll modification. You can use it as a reference in making your own scroll behavior that is consistent across iOS 5&6. In this code: When the user clicks on a text input box on a webpage, the code will stop the default scrolling behavior by keeping the page scrolled to its current position. Unfortunately, there is no way to cancel all scrolling, but rather you have to override scrolls. Once the default scrolling has been "suppressed", it gets the position of the active input box and scrolls to that position. This will put the active input box in the top of the screen. You can modify this according to your preferences. For example, you can use [UIScrollView scrollRectToVisible] to make it more like iOS5.

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate> {
    CGPoint origin;
    CGPoint activeElementOrigin;
    BOOL pauseScroll;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
    [self.webView loadRequest:request];
    self.webView.scrollView.delegate = self;

    pauseScroll = NO;
    origin = CGPointZero;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidShow)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
}

- (void)keyboardWillShow {
    NSLog(@"keyboard");
    pauseScroll = YES;
    origin = self.webView.scrollView.contentOffset;
}

- (void)keyboardDidShow {
    NSLog(@"keyboard DID SHOW");
    pauseScroll = NO;
    [self.webView.scrollView setContentOffset:activeElementOrigin animated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"scroll");

    if (pauseScroll) {
        [self.webView.scrollView setContentOffset:origin animated:NO];

        NSString *javaScript = [NSString stringWithFormat:@"function f(){ var textField = document.activeElement; return textField.getBoundingClientRect().top; } f();"];
        NSString *textFieldRectTop = [self.webView stringByEvaluatingJavaScriptFromString:javaScript];
        activeElementOrigin = origin;
        activeElementOrigin.y = [textFieldRectTop floatValue]-10;
    }
}


@end

如果这段代码对你有帮助,那么奖励我的赏金将是非常好的。
我会谦卑地欣赏。

If this code helped you, it would be very nice of you to reward the bounty to me. I would be humbly appreciative.

这篇关于UIWebView仅在输入焦点iOS 6上向下滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆