Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView's scrolling after textfield focus

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?

like image 881
Piotr Avatar asked Aug 29 '11 12:08

Piotr


4 Answers

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”

like image 116
rob mayoff Avatar answered Oct 15 '22 04:10

rob mayoff


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;
}
like image 22
HansPinckaers Avatar answered Oct 15 '22 04:10

HansPinckaers


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.

like image 2
Raphael Ayres Avatar answered Oct 15 '22 04:10

Raphael Ayres


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. ;-)

like image 1
Szamanm Avatar answered Oct 15 '22 04:10

Szamanm