Prevent UIWebView for Repositioning for input field

让人想犯罪 __ 提交于 2019-11-30 15:27:03
celestarry
  1. Add UIKeyboardWillShowNotification to NSNotificationCenter in viewDidLoad

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

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

This does work for me.

I'm not an Objective-C programmer (in fact I don't know Objective-C at all :-), but I needed to do this as well (in my web application running on iPad in a WebView), so with a "little" help of Google 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 that this isn't the best solution - but it works. From the point of view of a general programming (I don't know Objective-C) i guess it may be possible to overwrite setContentOffset method of UIScrollView class and implement your own behaviour (and possibly calling a super method of a parent).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!