SnapKit snp 无法分配给 UIHostingController
SnapKit snp can't assign to UIHostingController
我在很多项目中都使用 SnapKit。在新项目上也是如此。在项目中,我有 ViewController 与 SwiftUI 视图连接:
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")
}
}
但它抛出错误:
Value of type 'UIHostingController' has no member 'snp'
如何正确使用SnapKit?
UIHostingController
只是 UIViewController
的子类,它在常规 UIView
中呈现 SwiftUI 视图。如果你想设置约束,那么你应该像我们通常对视图所做的那样使用vc.view
。
我在很多项目中都使用 SnapKit。在新项目上也是如此。在项目中,我有 ViewController 与 SwiftUI 视图连接:
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")
}
}
但它抛出错误:
Value of type 'UIHostingController' has no member 'snp'
如何正确使用SnapKit?
UIHostingController
只是 UIViewController
的子类,它在常规 UIView
中呈现 SwiftUI 视图。如果你想设置约束,那么你应该像我们通常对视图所做的那样使用vc.view
。