Skip to content

Instantly share code, notes, and snippets.

@nteissler
Created June 4, 2024 16:17
Show Gist options
  • Save nteissler/9bd1a74e5747f01d27f20c97b7c5e0da to your computer and use it in GitHub Desktop.
Save nteissler/9bd1a74e5747f01d27f20c97b7c5e0da to your computer and use it in GitHub Desktop.
An demo of preferredCompactColumn
import SwiftUI
struct Day16: View {
@State private var sidebarSelection: Int?
@State private var path = NavigationPath()
/// Acts as a floor on what column is on top when collapsed
@State private var topColumn: NavigationSplitViewColumn = .content
@State private var vis: NavigationSplitViewVisibility = .all
var body: some View {
NavigationSplitView(
columnVisibility: $vis, preferredCompactColumn: $topColumn
) {
List(selection: $sidebarSelection) {
NavigationLink("Push 30", value: 30)
}
} content: {
if let sidebarSelection {
NavigationLink("Push View-Destination") {
NavigationLink("Push 'Hello!'", value: "Hello!")
.navigationDestination(for: String.self) {
DismissibleView(
text: "Pushed \($0)", color: .orange)
}
}
} else {
Text("Make a selection")
}
} detail: {
NavigationStack(path: $path) {
Text("Root")
}
}
.overlay(alignment: .bottomLeading) {
Text("Preferred Top: \(topColumn.description)")
.font(.headline)
}
}
}
private struct DismissibleView: View {
var text: String
var color: Color
@Environment(\.dismiss) var dismiss
var body: some View {
ZStack {
color
VStack {
Text(text)
Button("Dismiss") { dismiss() }
}
}
}
}
private extension NavigationSplitViewColumn {
var description: String {
switch self {
case .content: "content"
case .detail: "detail"
case .sidebar: "sidebar"
default: "unknown"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment