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).
source share