iOS 7 UIWebView键盘问题

前端之家收集整理的这篇文章主要介绍了iOS 7 UIWebView键盘问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须删除这个07/07这个吧,但对于iOS 7这个代码不起作用.

解决方法

我们用一些Objective C运行时欺骗来删除这个吧.

我们有一个类有一个方法

@interface _SwizzleHelper : NSObject @end

@implementation _SwizzleHelper

    -(id)inputAccessoryView
    {
        return nil;
    }

@end

一旦我们有一个网页视图,我们要从中删除该栏,我们迭代其滚动视图的子视图,并使用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);
}

相当于以上的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_getTypeEncoding(method))

            objc_registerClassPair(newClass!)

            swizzledClassMapping += [newClass!]
        }

        object_setClass(subview!,newClass!)
    }
}

猜你在找的iOS相关文章