美文网首页
Xcode 11 Xib UITextView 崩溃

Xcode 11 Xib UITextView 崩溃

作者: 天然酱油 | 来源:发表于2019-11-12 10:27 被阅读0次

    Xcode 11 Xib _UITextLayoutView 崩溃

    Xcode升级11.2之后,xib中使用UITextView突然闪退
    之后看到大神转的文章完美解决了问题,参考原文稍作了修改 https://blog.csdn.net/zzsatym/article/details/102919289

    我的崩溃信息如下:

     *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named _UITextLayoutView because no class named _UITextLayoutView was found; the class needs to be defined in source code or linked in from a library (ensure the class is part of the correct target)'
    *** First throw call stack:
    (0x1b4a0498c 0x1b472d0a4 0x1b48fa054 0x1b7fad6f8 0x1b7fada6c 0x1b7f49ec8 0x1b8e6d354 0x1b8e13f2c 0x1b8d09248 0x1046147e0 0x1046148a0 0x1b86bbc2c 0x1b7fad838 0x1b7f49ec8 0x1b86bfc7c 0x1b7fad838 0x1b7fada6c 0x1b7f49ec8 0x1b86bacc8 0x1b86bd8b8 0x1b8404b4c 0x1b84055d8 0x1b8405890 0x1b8405f94 0x1b8365804 0x1b8378de8 0x1b837a2ec 0x1b835e060 0x1b8e9d270 0x1bb3895f8 0x1bb38de28 0x1bb399894 0x1bb2e29f0 0x1bb30c890 0x1bb30d284 0x1b4981c48 0x1b497cb34 0x1b497d100 0x1b497c8bc 0x1be7e8328 0x1b8a126d4 0x10461cd10 0x1b4807460)
    libc++abi.dylib: terminating with uncaught exception of type NSException
    
    3个解决方案:
    1. UITextView使用纯代码可以有效避坑

    2. 下载其他版本Xcode(个人觉得没必要)

    3. 使用Runtime黑魔法
      创建 文件 UITextViewWorkaround 继承 NSObject

    • swift
    import UIKit
    
    @objcMembers
    class UITextViewWorkaround: NSObject {
           static func executeWorkaround() {
                if #available(iOS 13.2, *) {
                } else {
                    let className = "_UITextLayoutView"
                    let theClass = objc_getClass(className)
                    if theClass == nil {
                        let classPair: AnyClass? = objc_allocateClassPair(UIView.self, className, 0)
                        objc_registerClassPair(classPair!)
                    }
                }
            }
    }
    
    • OC .m
    
    #import "UITextViewWorkaround.h"
    #import  <objc/runtime.h>
     
    @implementation UITextViewWorkaround
     
    + (void)executeWorkaround {
        if (@available(iOS 13.2, *)) {
        }
        else {
            const char *className = "_UITextLayoutView";
            Class cls = objc_getClass(className);
            if (cls == nil) {
                cls = objc_allocateClassPair([UIView class], className, 0);
                objc_registerClassPair(cls);
    #if DEBUG
                printf("added %s dynamically\n", className);
    #endif
            }
        }
    }
     
    @end
    

    最后在AppDelegate调用即可

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            UITextViewWorkaround.executeWorkaround()
            return true
        }
    

    感谢作者 https://blog.csdn.net/zzsatym/article/details/102919289

    相关文章

      网友评论

          本文标题:Xcode 11 Xib UITextView 崩溃

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