带有 Swift UIView 和 .xib 文件的 React Native 模块

问题描述 投票:0回答:1

我正在构建一个自定义 RN 模块,我想将我的库中的 Swift UIViews 公开给其他人以在他们的 RN 应用程序中使用。我有一个使用 Swift UIView 类的基本示例,显示在我的 RN 模块示例应用程序中:

@objc(TestViewManager)
class TestViewManager : RCTViewManager {
    
  override static func requiresMainQueueSetup() -> Bool {
    return true
  }

  override func view() -> UIView! {
    //let myCustomView: TestView = UIView.fromNib()
    return TestView()
  }
}

当我尝试使用 nib 加载 TestView(使用 myCustomView fromNib() init)时,RN 应用程序会构建并运行,但随后崩溃。这是我的 UIView 扩展:

extension UIView {
    class func fromNib<T: UIView>() -> T {
        return Bundle(for: T.self).loadNibNamed(String(describing: T.self), owner: nil, options: nil)![0] as! T
    }
}

TestView.swift - 我的

.xib
文件名为 TestView.xib 并将文件所有者设置为 TestView.swift:

public class TestView: UIView {

    override init(frame: CGRect) {
      super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
    }

}

RN 模块中“允许”使用 .xib 文件吗?我找不到任何关于此的文档。有人有任何建议或知道这是否可能吗?

swift react-native uiview xib react-native-modules
1个回答
0
投票

您需要将 xib 放入捆绑包并使用捆绑包加载 xib

© www.soinside.com 2019 - 2024. All rights reserved.