-1

There are many tutorials out there on how to show a UIViewControllerRepresentable (e.g. to show a VNDocumentCameraViewController) as a sheet, but is there any way to show it as a regular view using NavigationLink, or just including it in another view.

Currently, if I include it like this:

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

The view shows up like this (embedded in the tab navigation view instead of taking up the whole screen) and the Save button (completion) does not work: NavigationLink UI result

If I include it straight in the view, Xcode gives me an "Initializer is never used" error and it does not show up in the view.

Stone
  • 309
  • 2
  • 3
  • 12
  • If you include it in a `Void` like an `action` it will say not used but if you but it in the `body` it will work – lorem ipsum Jan 10 '23 at 16:57

1 Answers1

0

To push onto a nav stack, it should be as simple as hiding the navigation bar on the DocumentCamera view. I'm using the implementation here, and with a View as follows:

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
                    
                }
                .ignoresSafeArea()
                .navigationBarHidden(true)
            }
        }
    }
}

which gives a result like this:

enter image description here enter image description here

To include the DocumentCamera in another view:


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
                }
            }
        }
    }
}

which gives…

enter image description here enter image description here

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • Thank you, but this still doesn't make the camera view full screen (for an example of what I'm talking about see the Notes app document scanner). Is there any method to achieve that? Accepting answer since it does solve most of my problems – Stone Jan 15 '23 at 21:40
  • 1
    OK, I see the problem - you just need to add `.ignoresSafeArea()`, see my updated answer. This lets the image fill the screen completely. – Ashley Mills Jan 16 '23 at 11:01