如何让WKWebView在出现键盘-ObjC&Javascript时不起来?

时间:2019-04-20 03:42:36

标签: javascript ios objective-c wkwebview

我有一个问题,想让WkWebView在出现键盘时不起来。
我希望我的WKWebView锁定位置。
有修复我代码的想法吗?
或任何JavaScript代码都可以实现这一目标。
谢谢。

@property (strong, nonatomic) IBOutlet WKWebView *webView;

- (void)viewDidLoad {
    [super viewDidLoad];

 [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(keyboardShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

}

- (void)keyboardShow:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGFloat keyboardHeight = UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?keyboardRect.size.height:keyboardRect.size.width;
    CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGFloat animationStyle = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] floatValue];

    self.offset = self.webView.scrollView.contentOffset;
    UIEdgeInsets contentInsets = self.webView.scrollView.contentInset;
    contentInsets.bottom = keyboardHeight;

    [UIView animateWithDuration:duration delay:0 options:animationStyle animations:^{
        self.webView.scrollView.contentInset = contentInsets;
    } completion:nil];
}

- (void)keyboardHide:(NSNotification *)notification {
    NSDictionary *userInfo = [notification userInfo];
    CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGFloat animationStyle = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] floatValue];

    UIEdgeInsets contentInsets = self.webView.scrollView.contentInset;
    contentInsets.bottom = 0;

    [UIView animateWithDuration:duration delay:0 options:animationStyle animations:^{
        self.webView.scrollView.contentInset = contentInsets;
        self.webView.scrollView.contentOffset = self.offset;
    } completion:nil];
}

0 个答案:

没有答案