How do I remove the sidebar toggle from a NavigationSplitView on iPad in landscape mode but keep it in portrait mode? Similar to how it works in the home app in iOS 17.
I have this so far but it doesn't work correctly at first launch before switching orientation once:
struct SidebarMainView: View {
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
@State private var isPortrait = true
var body: some View {
NavigationSplitView {
List {
Text("1")
Text("2")
Text("3")
}
.listStyle(SidebarListStyle()).toolbar(removing: isPortrait ? .none : .sidebarToggle).navigationTitle("HP").onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
guard let scene = UIApplication.shared.windows.first?.windowScene else { return }
self.isPortrait = scene.interfaceOrientation.isPortrait
}
} detail: {
Text("Select a department: \(self.isPortrait ? "yes" : "no")")
}
}
}
For reference, this is what the Home app looks like in different layouts. Note the forced and optional sidebar in each layout. I want to accomplish the same.






For iOS 17+
We can use
UIDevice.orientationDidChangeNotificationto detect current orientation andtoolbar(removing:)for removing sidebar toggle button.Hope It Helps !