首页 > 解决方案 > ios12中的点击偏移问题

问题描述

我正在处理在 webview 中运行的表单。在本机 ios 弹出窗口之一出现并关闭后,例如当键盘出现在文本区域中或下拉弹出窗口时,会出现点击问题。敲击中有一个偏移量,大约是键盘/下拉弹出窗口的高度。因此,当我点击表单上的一个点时,会按下一个向下 200-300 像素的不同组件。它只出现在 ios12 中。我找到的唯一解决方法是夹出和夹入。您对解决方案有什么建议吗?

标签: iosoffsettapios12

解决方案


这确实对我有用。

视图控制器.h

@property (nonatomic) CGPoint lastContentOffset;

视图控制器.m

- (void)viewDidLoad {    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}

- (void)onKeyboardWillShow:(NSNotification *)notification {
    self.lastContentOffset = self.webView.scrollView.contentOffset;
}

- (void)onKeyboardWillHide:(NSNotification *)notification {
    if (!CGPointEqualToPoint(self.lastContentOffset, self.webView.scrollView.contentOffset)) {
        [self.webView.scrollView setContentOffset:self.lastContentOffset];
        [self.webView.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }

    if (!CGPointEqualToPoint(self.lastContentOffset, self.webView.scrollView.contentOffset)) {
        [self.webView.scrollView setContentOffset:self.lastContentOffset];
        [self.webView.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
 }

推荐阅读