iOS 7 UIWebView 键盘问题 [英] iOS 7 UIWebView keyboard issue

查看:18
本文介绍了iOS 7 UIWebView 键盘问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在此处删除此栏 link 但对于 iOS 7 此代码不起作用.

I have to remove this bar as here link but for iOS 7 this code does not work.

推荐答案

我们通过一些 Objective C 运行时技巧移除了这个栏.

We remove this bar with some Objective C runtime trickery.

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

We have a class which has one method:

@interface _SwizzleHelper : NSObject @end

@implementation _SwizzleHelper

    -(id)inputAccessoryView
    {
        return nil;
    }

@end

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

Once we have a web view which we want to remove the bar from, we iterate its scroll view's subviews and take the UIWebDocumentView class. We then dynamically make the superclass of the class we created above to be the subview's class (UIWebDocumentView - but we cannot say that upfront because this is private API), and replace the subview's class to our class.

#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 中的等价物:


The equivalent of the above in 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 = "	ype(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!)
    }
}

这篇关于iOS 7 UIWebView 键盘问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆