Scrolling UIWebView after focus of text field

I have UIView and UIWebView on the screen. When I click on the text box on a website, the content of the web content grows. How can I make UIView move as well?

+6
source share
5 answers

You can subscribe to UIKeyboardWillShowNotification or UIKeyboardDidShowNotification and migrate UIView upon receipt of a notification. This process is described here:

Text, Web, and Editing Programming Guide for iOS: "Moving Content Under the Keyboard"

+7
source

Perhaps this helps: I did not want the UIWebView to scroll at all, including when focusing on the text field.

You must be a UIWebView delegate:

 _webView.scrollView.delegate = self; 

And then add this method

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { scrollView.contentOffset = CGPointZero; } 
+5
source

UIWebView has a callback:

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

This is triggered whenever a new request for a URL is collected. From javascript, you can trigger a new request URL in the onfocus event of the tag using a special scheme, for example:

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

Here's a script to post your custom event on any html page to load.

You will need to get all the HTML before loading it into a UIWebView as follows:

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

Then paste the following into the HTML text in the right 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, in 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; } 

What is it. Hope this helps.

+2
source

Wow, I had the same problem a few days ago, it was very unpleasant. I realized that window.yPageOffset changing, but as far as I know, there are no bindings to events when it changes. But maybe this will help you somehow. ;-)

+1
source

I think you rewrote scrollViewDidScroll incorrectly. You need to implement a custom class for UIWevView and overwrite scrollViewDidScroll :

  - (void) scrollViewDidScroll:(UIScrollView *)scrollView{ [super scrollViewDidScroll:scrollView]; [((id<UIScrollViewDelegate>)self.delegate) scrollViewDidScroll:scrollView]; } 
0
source

Source: https://habr.com/ru/post/896122/


All Articles