在WKWebView中隐藏键盘附件栏

时间:2015-09-13 04:45:46

标签: ios xcode uiwebview wkwebview

如何在WKWebview中隐藏键盘附件栏?虽然UIWebView有一些资源,但我还没有能够在Stackoverflow上为WkWebview找到一个。

相关:

  1. Removing the accesory item on the UIWebView keyboard
  2. iOS 7 UIWebView keyboard issue

2 个答案:

答案 0 :(得分:11)

WKWebView可以使用inputAccessoryView UIWebView上的@interface _NoInputAccessoryView : NSObject @end @implementation _NoInputAccessoryView - (id)inputAccessoryView { return nil; } @end 的方法变体。

首先,添加这个小课程:

- (void)removeInputAccessoryViewFromWKWebView:(WKWebView *)webView {
    UIView *targetView;

    for (UIView *view in webView.scrollView.subviews) {
        if([[view.class description] hasPrefix:@"WKContent"]) {
            targetView = view;
        }
    }

    if (!targetView) {
        return;
    }

    NSString *noInputAccessoryViewClassName = [NSString stringWithFormat:@"%@_NoInputAccessoryView", targetView.class.superclass];
    Class newClass = NSClassFromString(noInputAccessoryViewClassName);

    if(newClass == nil) {
        newClass = objc_allocateClassPair(targetView.class, [noInputAccessoryViewClassName cStringUsingEncoding:NSASCIIStringEncoding], 0);
        if(!newClass) {
            return;
        }

        Method method = class_getInstanceMethod([_NoInputAccessoryView class], @selector(inputAccessoryView));

        class_addMethod(newClass, @selector(inputAccessoryView), method_getImplementation(method), method_getTypeEncoding(method));

        objc_registerClassPair(newClass);
    }

    object_setClass(targetView, newClass);
}

接下来,添加此方法:

WKWebView

然后,您所要做的就是调用该方法并传入[self removeInputAccessoryViewFromWKWebView:webView];

UIWebView

注意:我还不确定这是否会通过应用审核,但它非常类似于我用于<div ng-switch="column"> <button ng-switch-when="XX_FILL OPEN" >Test Button</button> <input ng-switch-when="XX_CANCEL" type="checkbox" /> <span ng-switch-default>{{row[column]}}</span> </div> 的相同代码,并且确实通过了审核。

更新:此代码位于已通过App Store审核的应用中。

答案 1 :(得分:0)

在Swift4之后

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
     }

    @objc func keyboardWillAppear(_ notification: Notification) {
        //Do something here
        keyboardShowAndHide(true, notification: notification)
    }

    @objc func keyboardWillDisappear(_ notification: Notification) {
        //Do something here
        keyboardShowAndHide(false, notification: notification)
    }
    func keyboardShowAndHide(_ open: Bool, notification: Notification) {
      let userInfo = notification.userInfo ?? [:]
        let keyboardFrame = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
      let height = (keyboardFrame.height + 20) * (open ? 1 : -1)
        WKWebView.scrollView.contentInset.bottom += height
        WKWebView.scrollView.scrollIndicatorInsets.bottom += height
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //remove keyboard Listener
        NotificationCenter.default.removeObserver(self)
    }