SnapKit snp无法分配给UIHostingController

时间:2020-07-18 12:10:14

标签: swift swiftui snapkit

在许多项目中,我都使用SnapKit。在新项目上也是如此。在项目上,我有与SwiftUI视图连接的ViewController:

class OfficeListViewController: UIViewController {
 override func viewDidLoad() {
   super.viewDidLoad()
   let vc = UIHostingController(rootView: OfficeListView())
   addChild(vc)
   view.addSubview(vc.view)
   vc.didMove(toParent: self)
   vc.view.translatesAutoresizingMaskIntoConstraints = false

   // Here I want to set constraints to vc
   vc.snp // throws error: Value of type 'UIHostingController<OfficeView>' has no member 'snp'
 }
}

struct OfficeListView: View {
  var body: some View {
    Text("View")
  }
}

但是会引发错误:

'UIHostingController'类型的值没有成员'snp'

如何正确使用SnapKit?

1 个答案:

答案 0 :(得分:1)

UIHostingController只是UIViewController的子类,它在常规UIView内呈现SwiftUI视图。如果您要设置约束,则应像通常使用视图一样使用vc.view