Home > Net >  How to make the view full frame even if Sidebar is on in SwiftUI?
How to make the view full frame even if Sidebar is on in SwiftUI?

Time:01-08

Goal

Like the app Maps, views appear to be full frame size when Sidebar is open. Views are under Sidebar.

enter image description here

What I've tried

In order to maximize View's size, I've set the size to be infinity and add view modifier .ignoresSafeArea() but it's still not full frame like Maps

You will see the frame of view will be adjusted when I turn on the sidebar

enter image description here

var body: some View {
    NavigationView {
        List(0 ..< 5) { item in
            NavigationLink("Test") {
                VStack {
                    Text("Test")
                }
                .navigationTitle("Test")
                .frame(maxWidth: .infinity, maxHeight: .infinity)
                .ignoresSafeArea()
            }
        }
        ScrollView {
            Text("Hello, world!")
                .padding()
        }
    }
    .frame(minWidth: 400, minHeight: 400)
    .toolbar {
        ToolbarItem(placement: .navigation) {
            Button(action: toggleSidebar, label: {
                Image(systemName: "sidebar.leading")
            })
        }
    }
}

CodePudding user response:

Just use transparent content for right side and place map below NavigationView.

Here is a demo of approach. Tested with Xcode 13 / macOS 12

demo

var body: some View {
    ZStack {
        MapView()                // << content here !!
        NavigationView {
            List(0 ..< 5) { item in
                NavigationLink("Test") {
                    VStack {
                        Text("Test")
                    }
                    .navigationTitle("Test")
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .ignoresSafeArea()
                }
            }
            Color.clear         // << transparent right !!
        }
        .frame(minWidth: 400, minHeight: 400)
        .toolbar {
            ToolbarItem(placement: .navigation) {
                Button(action: toggleSidebar, label: {
                    Image(systemName: "sidebar.leading")
                })
            }
        }
    }
}
  •  Tags:  
  • Related