Skip to content

Instantly share code, notes, and snippets.

@StewartLynch
Created July 5, 2022 00:04
Show Gist options
  • Save StewartLynch/d5549de753e36dcb03d273328096f0bb to your computer and use it in GitHub Desktop.
Save StewartLynch/d5549de753e36dcb03d273328096f0bb to your computer and use it in GitHub Desktop.
// How can I transition from the sidebar view to the contentView on iPhone?
import SwiftUI
struct Name: Identifiable, Hashable {
let id = UUID()
var firstName: String
var lastName: String
static var sample: [Name] {
[
Name(firstName: "Stewart", lastName: "Lynch"),
Name(firstName: "Emily", lastName: "Heidrich"),
Name(firstName: "Adian", lastName: "Lynch")
]
}
}
struct ContentView: View {
@State private var columnVisibility: NavigationSplitViewVisibility = .all
@State private var names = Name.sample
@State private var selectedName: Name?
var body: some View {
NavigationSplitView(columnVisibility: $columnVisibility) {
//Sidebar
HStack {
Button {
names = names.filter {$0.lastName == "Lynch"}
} label: {
Text("Lynch Names")
}
Button {
names = Name.sample
} label: {
Text("All Names")
}
}
.buttonStyle(.bordered)
} content: {
List(names, selection: $selectedName) { name in
NavigationLink(value: name) {
Text(name.firstName)
}
}
} detail: {
ZStack {
if let selectedName {
Text(selectedName.lastName)
} else {
Text("Select a name")
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment