macosswiftuimacos-big-sur

SwiftUI macOS right sidebar inspector


I have a document-based SwiftUI app. I'd like to make a inspector sidebar like the one in Xcode.

Starting with Xcode's Document App template, I tried the following:

struct ContentView: View {
    @Binding var document: DocumentTestDocument
    @State var showInspector = true

    var body: some View {
        HSplitView {
            TextEditor(text: $document.text)
            if showInspector {
                Text("Inspector")
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
        }
        .toolbar {
            Button(action: { showInspector.toggle() }) {
                Label("Toggle Inspector", systemImage: "sidebar.right")
            }
        }
    }
}

Which yielded:

Screenshot

How can I extend the right sidebar to full height like in Xcode?

NavigationView works for left-side sidebars, but I'm not sure how to do it for right-side sidebars.


Solution

  • Still in beta but out soon with iOS 17, tvOS17 and macOS14, SwiftUI will provide exactly this type of inspector:

    ContentView()
        .inspector($showInspector) {
            InspectorView()
        }
    

    Here is a video from WWDC23: https://developer.apple.com/videos/play/wwdc2023/10161

    And a link to the docs: https://developer.apple.com/documentation/corelocationui/locationbutton/4200336-inspector/