Prevent UIWebView from being re-hosted for input field

I have an iPad application in which I show the input form as a UIWebView in a UIPopoverController. My popover controller is so large that it doesnโ€™t need to be changed when the keyboard appears.

When I click in the input box in UIWebView, the web view advances further when the keyboard appears. It is pushed to such an extent that the text field is at the top of the frame. (This is the standard behavior that you see when using forms in a mobile safari).

The webview does not scroll at all before the keyboard is up, but as soon as it appears (as shown in the second image), I can scroll the webview to the desired position.

Since my webview is already set up correctly, I don't want this behavior. Does anyone have any suggestions on how to prevent this? (Not using a web view here is currently not an option)

Thanks!

View without keyboardView with keyboard

+4
source share
2 answers
  • Add UIKeyboardWillShowNotification to NSNotificationCenter in viewDidLoad

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
  • Embed keyboardWillShow: and readjustWebviewScroller methods

     - (void)keyboardWillShow:(NSNotification *)aNotification { [self performSelector:@selector(readjustWebviewScroller) withObject:nil afterDelay:0]; } - (void)readjustWebviewScroller { _webView.scrollView.bounds = _webView.bounds; } 

This works for me.

+3
source

I'm not an Objective-C programmer (in fact, I donโ€™t know Objective-C at all :-), but I also needed to do this (in my web application running on an iPad in WebView), so with the help of Googleโ€™s โ€œlittleโ€ help I did this:

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

I know this is not the best solution, but it works. From a general programming point of view (I don't know Objective-C), I assume that it may be possible to overwrite the setContentOffset method of the UIScrollView class and implement your own behavior (and possibly call the super parent method).

0
source

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


All Articles