iOS 7 UIWebView键盘问题

Mat*_*der 13 uiwebview ios ios7

我必须删除此栏作为此链接,但对于iOS 7,此代码不起作用.

Leo*_*ica 25

我们用一些Objective C运行时技巧删除了这个栏.

我们有一个类有一个方法:

@interface _SwizzleHelper : NSObject @end

@implementation _SwizzleHelper

    -(id)inputAccessoryView
    {
        return nil;
    }

@end
Run Code Online (Sandbox Code Playgroud)

一旦我们有了一个我们想要删除栏的Web视图,我们就会迭代它的滚动视图的子视图并获取UIWebDocumentView该类.然后,我们动态地将上面创建的类的超类作为子视图的类(UIWebDocumentView - 但我们不能预先说明,因为这是私有API),并将子视图的类替换为我们的类.

#import "objc/runtime.h"    

-(void)__removeInputAccessoryView
{
    UIView* subview;

    for (UIView* view in self.scrollView.subviews) {
        if([[view.class description] hasPrefix:@"UIWeb"])
            subview = view;
    }

    if(subview == nil) return;

    NSString* name = [NSString stringWithFormat:@"%@_SwizzleHelper", subview.class.superclass];
    Class newClass = NSClassFromString(name);

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

        Method method = class_getInstanceMethod([_SwizzleHelper class], @selector(inputAccessoryView));
        class_addMethod(newClass, @selector(inputAccessoryView), method_getImplementation(method), method_getTypeEncoding(method));

        objc_registerClassPair(newClass);
    }

    object_setClass(subview, newClass);
}
Run Code Online (Sandbox Code Playgroud)

相当于Swift 3.0中的上述内容:

import UIKit
import ObjectiveC

var swizzledClassMapping = [AnyClass]()

extension UIWebView {
    func noInputAccessoryView() -> UIView? {
        return nil
    }

    public func removeInputAccessoryView() {
        var subview: AnyObject?

        for (_, view) in scrollView.subviews.enumerated() {
            if NSStringFromClass(type(of: view)).hasPrefix("UIWeb") {
                subview = view
            }
        }

        guard subview != nil else {
            return
        }

        //Guard in case this method is called twice on the same webview.
        guard !(swizzledClassMapping as NSArray).contains(type(of: subview!)) else {
            return;
        }

        let className = "\type(of: subview!)_SwizzleHelper"
        var newClass : AnyClass? = NSClassFromString(className)

        if newClass == nil {
            newClass = objc_allocateClassPair(type(of: subview!), className, 0)

            guard newClass != nil else {
                return;
            }

            let method = class_getInstanceMethod(type(of: self), #selector(UIWebView.noInputAccessoryView))
            class_addMethod(newClass!, #selector(getter: UIResponder.inputAccessoryView), method_getImplementation(method), method_getTypeEncoding(method))

            objc_registerClassPair(newClass!)

            swizzledClassMapping += [newClass!]
        }

        object_setClass(subview!, newClass!)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,我们的应用程序的代码与此类似,并且已获得批准. (3认同)
  • 把它放在你的`UIWebView`子类中.你还需要在你的文件顶部`#import"objc/runtime.h"` (2认同)
  • @iDhaval我们的应用已经获得多年的认可. (2认同)