Turn off sidebar hiding on NavigationSplitView in SwiftUI
Published on: May 21, 2024By default, a NavigationSplitView
in SwiftUI will show users an option to toggle the visibility of the sidebar. If you want to prevent this button from showing up so that users will always have to see your sidebar, you can do this by applying the toolbar(removing:)
view modifier to your split view's sidebar as follows:
NavigationSplitView(sidebar: {
ExercisesList()
.toolbar(removing: .sidebarToggle)
}, detail: {
ExerciseDetail(exercise: exercises.first!)
})
The downside of doing this is that the button is hidden both in portrait and landscape modes. The result is that landscape users can no longer access your app's sidebar.
To fix this you can try applying the view modifier conditionally based on the device's orientation but that's not ideal; apps in landscape might also show the split view as a single column. I have yet to find a good, reliable solution to conditionally presenting and hiding the sidebar toggle.
One upside for users is that they can still summon the sidebar in portrait mode by swiping from the leading edge of the screen towards the middle. It's not perfect, but it's better than nothing I suppose.