UIWebView's scrolling after textfield focus

2019-04-28 16:04发布

I have UIView and UIWebView on screen. When I click on text field in website, web view content is going up. How could I force UIView to move as well then?

5条回答
ゆ 、 Hurt°
2楼-- · 2019-04-28 16:23

You can subscribe to either the UIKeyboardWillShowNotification or the UIKeyboardDidShowNotification, and move your UIView when you receive the notification. This process is described here:

Text, Web, and Editing Programming Guide for iOS: “Moving Content That Is Located Under the Keyboard”

查看更多
ゆ 、 Hurt°
3楼-- · 2019-04-28 16:25

Wow, I had the same problem few days ago, it was really annoying. I figured out that window.yPageOffset is changing, but as far as I know there aren't any events to bind when it changes. But maybe it will help you somehow. ;-)

查看更多
放我归山
4楼-- · 2019-04-28 16:36

Maybe this helps: I didn't want the UIWebView to scroll at all, including when focusing on a textfield.

You have to be the delegate of the UIWebView:

_webView.scrollView.delegate = self;

And then add this method

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    scrollView.contentOffset = CGPointZero;
}
查看更多
Emotional °昔
5楼-- · 2019-04-28 16:36

I think you overwrote scrollViewDidScroll wrong. You need to implement custom class for UIWevView and overwrite scrollViewDidScroll:

 - (void) scrollViewDidScroll:(UIScrollView *)scrollView{
        [super scrollViewDidScroll:scrollView];
        [((id<UIScrollViewDelegate>)self.delegate) scrollViewDidScroll:scrollView];
    }
查看更多
forever°为你锁心
6楼-- · 2019-04-28 16:40

UIWebView has a callback:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

This is triggered whenever a new URL request is about to load. From javascript, you could trigger a new request URL on the onfocus event of the tag, with a custom schema like:

window.location = "webViewCallback://somefunction";

Here's a script to put your custom event inside any html page to load.

You'll have to get the whole HTML before loading it to the UIWebView like this:

NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"your URL"] encoding:NSUTF8StringEncoding error:nil];

Then insert the following inside the HTML text in a appropriate place:

<script>
    var inputs = document.getElementsByTagName('input');

    for(int i = 0; i < inputs.length; i++)
    {
        if(inputs[i].type = "text")
        {
             inputs[i].onfocus += "javascript:triggerCallback()";
        }
    }

 function triggerCallback()
 {
      window.location = "webViewCallback://somefunction";
 }
</script>

Then, on the callback you should do something like this:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( [[inRequest URL] scheme] == @"webViewCallback" ) {

        //Change the views position 

        return NO;
    }

    return YES;
}

That's it. Hope it helps.

查看更多
登录 后发表回答