Post

Replies

Boosts

Views

Activity

Unable to set initial selection state of SwiftUI `List` in iOS 14
I'm using a NavigationView to display "split view" style sidebar and secondary views similar to the Fruta sample app. My sidebar has a List of NavigationLink's, and my secondary view displays that linked content. However, I'm not able to set an initial "default" selection (i.e. blue background selected row style) for the List in my sidebar. When the app first launches, there's no selection state in the List for the secondary view that is displayed until the user taps a row of the List. I have a @State var called selection that's passed into my Sidebar, which references it as a @Binding. selection has a value, which is why it's unexpected that the resulting List doesn't show it as selected when first displayed. EXPECTED RESULTS When the app launches, I would expect the List selects the initial row matching the @Binding that's passed in. ACTUAL RESULTS When the app launches, The List has no initial selected row until the user taps a row. I'd like for the user to launch into the app with a Sidebar List row already selected, similar to the way the Shortcuts app does when launched. How do I enable this kind of behavior? Here's full demo code – iOS 14 beta 4, iPad in landscape: import SwiftUI @main struct SidebarSelectionApp: App { 		var body: some Scene { 				WindowGroup { 						ContentView() 				} 		} } enum NavigationItem: Int, Identifiable { 		var id: Int { return self.rawValue } 		case view1 		case view2 		case view3 } struct ContentView: View { 		@State var selection: NavigationItem? = .view1 		var body: some View { 				Sidebar(selection: $selection) 		} } struct Sidebar: View { 		@Binding var selection: NavigationItem? 		var body: some View { 				NavigationView { 						list 						EmptyView() 				} 		} 		var list: some View { 				List(selection: $selection) { 						NavigationLink( 								destination: View1(), 								tag: NavigationItem.view1, 								selection: $selection, 								label: { 										Text("View 1") 						}) 						NavigationLink( 								destination: View2(), 								tag: NavigationItem.view2, 								selection: $selection, 								label: { 										Text("View 2") 						}) 						NavigationLink( 								destination: View3(), 								tag: NavigationItem.view3, 								selection: $selection, 								label: { 										Text("View 3") 						}) 				} 				.listStyle(SidebarListStyle()) 				.navigationTitle("List") 		} } struct View1: View { 		var body: some View { 				Text("This is View 1").font(.title).foregroundColor(.green) 		} } struct View2: View { 		var body: some View { 				Text("This is View 2").font(.title).foregroundColor(.blue) 		} } struct View3: View { 		var body: some View { 				Text("This is View 3").font(.title).foregroundColor(.red) 		} }
4
1
2.9k
Aug ’20