我们的项目是RN+原生混合工程,最近遇到了使用xcode11打出的包直接闪退了,报错内容是:
Client error attempting to change layout margins of a private view
从字面上的大概意思是尝试访问改变一个私有的属性而导致的错误,然后直接调试报错地址是在[self.navigationController setNavigationBarHidden:YES animated:YES];这里报错了。
于是网上查了下原来是ios13的一个bug,是访问导航条一个内部View属性时报错了。
解决方案如下:
UIApplication+Swizzle.swift 文件:
importUIKit
extension UIApplication {
privatestaticletclassSwizzedMethodRunOnce:Void= {
if#available(iOS11.0, *) {
UINavigationBar.swizzedMethod()
}
}()
openoverridevarnext:UIResponder? {
UIApplication.classSwizzedMethodRunOnce
return super.next
}
}
UINavigationBar+FixSpace.swift 文件:
importUIKit
funcswizzleMethod(for aClass:AnyClass, originalSelector:Selector, swizzledSelector:Selector) {
letoriginalMethod =class_getInstanceMethod(aClass, originalSelector)
letswizzledMethod =class_getInstanceMethod(aClass, swizzledSelector)
letdidAddMethod =class_addMethod(aClass, originalSelector,method_getImplementation(swizzledMethod!),method_getTypeEncoding(swizzledMethod!))
ifdidAddMethod {
class_replaceMethod(aClass, swizzledSelector,method_getImplementation(originalMethod!),method_getTypeEncoding(originalMethod!))
}else{
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
}
@available(iOS 11.0, *)
extension UINavigationBar {
staticfuncswizzedMethod() {
swizzleMethod(
for:UINavigationBar.self,
originalSelector:#selector(UINavigationBar.layoutSubviews),
swizzledSelector:#selector(UINavigationBar.swizzle_layoutSubviews))
}
@objcfuncswizzle_layoutSubviews() {
swizzle_layoutSubviews()
layoutMargins = .zero
forviewinsubviews{
ifNSStringFromClass(view.classForCoder).contains("ContentView") {
view.layoutMargins=UIEdgeInsets(top:0, left:0, bottom:0, right:0)
}
}
}
}
开build打包,解决了。
网友评论