I'm making an app that runs perfectly in iOS and iPadOS with SwiftUI, but in iPad there is a side bar that appears when I use .navigationViewStyle(DoubleColumnNavigationViewStyle())
modifier in my NavigationView
and I'm wondering if is there a way to close the side bar programmatically, usually users can close the sidebar by touching the button from top left of the screen, but that's user interaction. It's possible to close the side bar directly in code?
Find out that SwiftUI-Introspect can be used to accomplish this requirement, but I wanted to do it without external libraries.
This is a simple code to reproduce the sidebar in iPadOS:
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
NavigationView {
if UIDevice.current.userInterfaceIdiom == .pad {
Text("Sidebar here")
Text("Content here")
}
}.navigationViewStyle(DoubleColumnNavigationViewStyle())
}
}
}