Removing WKWebView Accesory bar in Swift

后端 未结 4 1774
臣服心动
臣服心动 2020-12-05 21:04

I am trying for a few days now to get this converted into Swift without really having much background with it.

This is what I\'ve got so far ... and I have been look

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 21:25

    This code snippet should get you over your issue:

    class _NoInputAccessoryView: NSObject {
    
        func removeInputAccessoryViewFromWKWebView(webView: WKWebView) {
            // make sure to make UIView an optional here...
            var targetView: UIView? = nil
            for view in webView.scrollView.subviews {
                if String(view.dynamicType).hasPrefix("WKContent") {
                    targetView = view
                }
            }
    
            // only optionals can be nil
            if targetView == nil {
                return
            }
    
            let noInputAccessoryViewClassName = "\(targetView!.superclass)_NoInputAccessoryView"
            var newClass : AnyObject? = NSClassFromString(noInputAccessoryViewClassName)
            if newClass == nil {
                let uiViewClass : AnyClass = object_getClass(targetView!)
                newClass = objc_allocateClassPair(uiViewClass, noInputAccessoryViewClassName.cStringUsingEncoding(NSASCIIStringEncoding)!, 0)
            }
        } 
    

    You can also use "String(view.dynamicType)" to get the class name of the object you're looking at, as I noticed via this answer as I was researching the way to solve your problem.

    Using hasPrefix like that in both Objective-C and Swift is really hacky and perhaps a better way of hiding the keyboard could be found for production code?

提交回复
热议问题