Problem description

Xcode12 is not currently available in an official release, but xcode-beta testing found that many children of the UITableViewCell in the project were unable to respond to gestures such as clicking or swiping.

AddSubView (tempView1) is used to add faulty cells. Was found by trying to view the system’s own cell. The contentView ` (class name UITableViewCellContentView) block at the bottom, so you need to “‘ cell. The contentView. AddSubView (tempView1).

I searched more than 200 places in the project that were written in this way, and divided into many different components, compatible with a lot of work.

So quick compatibility via Runtime in a simple and violent way, the principle is in all cells addSubView()Is intercepted by runtime insteadcontentView.addsubview(), the Demo code is as follows:
extension UITableViewCell {
    
    class func ios14Bug(a){
        
        let sel1 = #selector(UITableViewCell.runtime_addSubview(_:))
        let sel2 = #selector(UITableViewCell.addSubview(_:))
        
        let method1 = class_getInstanceMethod(UITableViewCell.self, sel1)!
        let method2 = class_getInstanceMethod(UITableViewCell.self, sel2)!
        
        let isDid: Bool = class_addMethod(self, sel2, method_getImplementation(method1), method_getTypeEncoding(method1))
        if isDid {
            class_replaceMethod(self, sel1, method_getImplementation(method2), method_getTypeEncoding(method2))
        } else {
            method_exchangeImplementations(method2, method1)
        }
    }
    
    @objc func runtime_addSubview(_ view: UIView) {
        / / judgment not let UITableViewCellContentView addSubView himself
        if view.isKind(of: NSClassFromString("UITableViewCellContentView")!) {
            runtime_addSubview(view)
        } else {
            self.contentView.addSubview(view)
        }
    }
}

Copy the code

At present, our project has been globally compatible in this way. If there is anything improper, welcome to discuss!