美文网首页
IOS - UIWebView隐藏工具条

IOS - UIWebView隐藏工具条

作者: 囧rg | 来源:发表于2017-05-03 15:06 被阅读135次

    最近研究富文本编辑器的web版实现时,发现输入键盘上的工具条隐藏不掉,试过了很多的方法,也无法实现。
    后来在一个富文本项目中,找到了解决的方案。

    .h文件

    #import <UIKit/UIKit.h>
    #import <objc/runtime.h>
    
    @interface UIWebView (Ext)
    
    @property (nonatomic, assign) BOOL hidesInputAccessoryView;
    
    @end
    
    

    .m文件

    #import "UIWebView+Ext.h"
    
    @implementation UIWebView (Ext)
    
    
    static const char * const hackishFixClassName = "UIWebBrowserViewMinusAccessoryView";
    static Class hackishFixClass = Nil;
    
    - (UIView *)hackishlyFoundBrowserView {
        UIScrollView *scrollView = self.scrollView;
        
        UIView *browserView = nil;
        for (UIView *subview in scrollView.subviews) {
            if ([NSStringFromClass([subview class]) hasPrefix:@"UIWebBrowserView"]) {
                browserView = subview;
                break;
            }
        }
        return browserView;
    }
    
    - (id)methodReturningNil {
        return nil;
    }
    
    - (void)ensureHackishSubclassExistsOfBrowserViewClass:(Class)browserViewClass {
        if (!hackishFixClass) {
            Class newClass = objc_allocateClassPair(browserViewClass, hackishFixClassName, 0);
            newClass = objc_allocateClassPair(browserViewClass, hackishFixClassName, 0);
            IMP nilImp = [self methodForSelector:@selector(methodReturningNil)];
            class_addMethod(newClass, @selector(inputAccessoryView), nilImp, "@@:");
            objc_registerClassPair(newClass);
            
            hackishFixClass = newClass;
        }
    }
    
    - (BOOL) hidesInputAccessoryView {
        UIView *browserView = [self hackishlyFoundBrowserView];
        return [browserView class] == hackishFixClass;
    }
    
    - (void) setHidesInputAccessoryView:(BOOL)value {
        UIView *browserView = [self hackishlyFoundBrowserView];
        if (browserView == nil) {
            return;
        }
        [self ensureHackishSubclassExistsOfBrowserViewClass:[browserView class]];
        
        if (value) {
            object_setClass(browserView, hackishFixClass);
        }
        else {
            Class normalClass = objc_getClass("UIWebBrowserView");
            object_setClass(browserView, normalClass);
        }
        [browserView reloadInputViews];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:IOS - UIWebView隐藏工具条

          本文链接:https://www.haomeiwen.com/subject/vvgktxtx.html