import SwiftUI
import Playgrounds
@main struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct AccountView: View{
var body: some View{
Text("111")
}
}
struct ChatView: View{
var body: some View{
Text("222")
}
}
struct ProjectView: View{
var body: some View{
Text("333")
}
}
struct EnvView: View{
var body: some View{
Text("444")
}
}
struct ContentView: View {
var body: some View {
NavigationSplitView {
List{
NavigationLink("Account",value: 1)
NavigationLink("Chat",value: 2)
NavigationLink("Project",value: 3)
NavigationLink("Enviroments",value: 4)
}
} detail: {
NavigationStack{
VStack{
Text("SB")
}
.navigationDestination(for: Int.self) { number in
switch number {
case 1: AccountView()
case 2: ChatView()
case 3: ProjectView()
case 4: EnvView()
default: Text("未知页面")
}
}
}
}
}
}
#Preview {
ContentView()
}
I want to write a sidebar to change pages, I wrote some codes by advices of Deepseek, but it didn't work, it just stay in the VStack under the contentView.
Help me😭
Thanks for the post.
You want to open with a new view? What about instead of navigationDestination you use a detail?
NavigationSplitView { { NavigationLink("Account", value: 1) } .navigationTitle("Menu") } detail: { switch here { case 1: AccountView() } }
the NavigationLink is in the sidebar, but the NavigationStack and .navigationDestination are in the detail area.
I do not know if you want to use a switch but I recommend it, in that case do not forget to add the
switch selection // and then each case
Hope this work, sorry I didn't use Xcode so you'll have to put it all together.
Hope this helps
Albert WWDR