【问题标题】:Is possible to combine together in same View SwiftUIView and UIViewControllerRepresentable?是否可以在同一个 View SwiftUIView 和 UIViewControllerRepresentable 中组合在一起?
【发布时间】:2020-06-15 01:16:53
【问题描述】:

我想知道是否可以将 View 和 UIViewControllerRepresentable 组合在同一个视图中。 我试过了:

//Here I declare MyViewController:
class MyViewController: UIViewController {
    override viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
    }
}

struct MyViewControllerIntegrate: UIViewControllerRepresentable {

    func makeUIViewController(context: UIViewControllerRepresentableContext<MyViewControllerIntegrate>) -> MyViewController {
        return MyViewController()
    }

    func updateUIViewController(_ uiViewController: MyViewController, context: UIViewControllerRepresentableContext<MyViewControllerIntegrate>) {
    }
}

struct MyView: View {
    var body: some View {
        Text("Hello StackOverflow!")
    }
}

struct ContentView: View {
    var body: some View {
        MyView()
        MyViewController()
        .frame(height: 400)
    }
}

Xcode 显示一条错误消息:

函数声明了一个不透明的返回类型,但它的主体中没有返回语句来推断基础类型

有什么提示吗?谢谢

【问题讨论】:

    标签: swift xcode view uiviewcontroller swiftui


    【解决方案1】:

    将它们放入 Group(或某些堆栈,例如 VStack)中

    struct ContentView: View {
        var body: some View {
            Group {
               MyView()
               MyViewController()
                  .frame(height: 400)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      相关资源
      最近更新 更多