在许多项目中,我都使用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?
答案 0 :(得分:1)
UIHostingController
只是UIViewController
的子类,它在常规UIView
内呈现SwiftUI视图。如果您要设置约束,则应像通常使用视图一样使用vc.view
。