DocumentGroupLaunchScene causes the navigation bar to disappear
Asked Answered
B

1

6

When using the new DocumentGroupLaunchScene in a document-based app, the documents are opened without a navigation bar. The document title and back button are not visible and the user cannot return back to the document browser to open another document.

@main
struct MyApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: MyDocument()) { file in
            ContentView(document: file.$document)
        }

        DocumentGroupLaunchScene {
            NewDocumentButton("Start")
        } background: {
            Color.blue
        }
    }
}

When I launch the app and create a document, I see this:

enter image description here

When I comment out DocumentGroupLaunchScene code, I get the navigation bar:

enter image description here

I'm using Xcode 16.0 beta 6 and a device with iOS 18.0 beta 8. I've filed a bug report to Apple.

Does somebody know a workaround?

Batory answered 30/8 at 8:0 Comment(3)
Same issue here. Could you solve it?Tramontane
@ViktorMaric: Not yet - I had to release the app without DocumentGroupLaunchScene. I haven't got a reply to my bug report either.Batory
I'm having the same issue, funny thing is. With the DocumentGroupLaunchScene it doesn't render the navigationBar, but it renders the BottomBar. If you remove the DocumentGroupLaunchScene it renders the navigationBar, but it doesn't render the BottomBar.Trinatte
F
3

This behaviour does not seem to be included in the DocumentGroup by default anymore. (I am unsure whether this is a SwiftUI bug or not.) We now have a couple of new modifiers which allow us to reintroduce the same navigation bar we had earlier: See Configure your app navigation titles for more details.

Share

A combination of .navigationTitle("My Title") and .navigationDocument(myURL) would allow to recreate the document title and the share behaviour.

We can access the fileURL through the DocumentGroup:

DocumentGroup(newDocument: PGNDocument()) { file in
  NavigationStack {
    ContentView(document: file.$document, url: file.fileURL!)
  }
}

Note: NavigationStack is important for our new navigation bar to show up in the first place.

Rename

We can add a Binding to .navigationTitle to allow renaming: .navigationTitle($contentTitle)

Note: I have to admit, I have yet to find out how to actually bind this to the name of document we're currently editing. Please let me know if you did find out.

Back button

The back button can be recreated by adding the following Toolbar to your view:

@Environment(\.dismiss) private var dismiss

var body: some View {
  Content
    .toolbar {
      ToolbarItem(placement: .navigation) {
        Button("Back", systemImage: "chevron.left") {
          dismiss()
        }
      }
    }
}
Finesse answered 19/9 at 15:8 Comment(4)
Lifesaver! Many thanks! I can find no documentation suggesting we now need to do this.Gorski
@Gorski I have to admit, neither do I. I just assumed it. I'll rephrase my answer to avoid any confusion.Trinatte
With regard to tracking document title changes, I've been using an onChange listener on my ContentView. DocumentGroup(newDocument: PGNDocument()) { file in ContentView() .onChange(of: file.fileURL, perform: { url in contentTitle = url.lastPathComponent } }Camelliacamelopard
This is broken again in the latest Xcode... This workaround will cause double navbar at the top, and double back button. And if we take the NavigationStack out, then it is broken again.Tramontane

© 2022 - 2024 — McMap. All rights reserved.