【问题标题】:How do you show a UIViewControllerRepresentable in SwiftUI as a non-sheet?如何在 SwiftUI 中将 UIViewControllerRepresentable 显示为非工作表?
【发布时间】:2023-01-11 02:15:30
【问题描述】:

有很多关于如何将 UIViewControllerRepresentable(例如,显示 VNDocumentCameraViewController)显示为工作表的教程,但是有没有办法使用 NavigationLink 将其显示为常规视图,或者只是将其包含在另一个视图中。

目前,如果我这样包含它:

NavigationLink("Add with camera", destination: ScannerView(completion: {result in  resultHandler(text: result)}))

视图显示如下(嵌入选项卡导航视图而不是占据整个屏幕)并且保存按钮(完成)不起作用:

如果我直接将它包含在视图中,Xcode 会给我一个“Initializer is never used”错误并且它不会显示在视图中。

【问题讨论】:

  • 如果你将它包含在 Void 中,就像 action 一样,它会说未使用,但如果你将它包含在 body 中,它将起作用

标签: swift swiftui uiviewcontrollerrepresentable


【解决方案1】:

要推送到导航堆栈,应该像在DocumentCamera 视图上隐藏导航栏一样简单。我正在使用实现 here,并使用如下视图:

struct ContentView: View {
    
    @State private var path: [String] = []
        
    var body: some View {
        NavigationStack(path: $path) {
            Form {
                NavigationLink("Show Camera", value: "Camera")
            }
            .navigationDestination(for: String.self) { _ in
                DocumentCamera {
                    path = []
                } resultAction: { _ in
                    
                }
                .navigationBarHidden(true)
            }
        }
    }
}

结果如下:

在另一个视图中包含 DocumentCamera


struct ContentView: View {
    
    @State private var showingCamera = false
        
    var body: some View {
        VStack {
            
            if showingCamera {
                DocumentCamera {
                    showingCamera = false
                } resultAction: { _ in
                    
                }
                .padding()
                .aspectRatio(0.6, contentMode: .fit)
            } else {
                Button("Show camera") {
                    showingCamera = true
                }
            }
        }
    }
}

这使…

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    相关资源
    最近更新 更多