How do I scroll a List to a specific item?

I have the following code working to scroll to a given item. But the combination of ScrollView + LazyStackview doesn't give me proper selection highlighting, like a List would. So my questions are:
  • is there a way to scroll a List to a specific item, i.e. can I use ScrollViewReader with Lists?

  • is there a way to emulate the row highlighting which you press on the cell? And to abort selection in the default way by scrolling a bit?

Code Block SwiftUI
struct ContentView: View {
let timezones = TimeZone.knownTimeZoneIdentifiers.map { TimeZone(identifier: $0)! }
var body: some View {
let continents = Set(timezones.compactMap { $0.continent }).sorted()
ScrollView {
ScrollViewReader { value in
LazyVStack(alignment: .leading, spacing: 5, pinnedViews: [.sectionHeaders]) {
ForEach(continents, id: \.self) { continent in
let continentTZ = timezones.filter { $0.continent == continent}
Section(header: HeaderView(title: continent)) {
ForEach(continentTZ) { timezone in
Button(action: {
// this will dismiss the selection
}) {
VStack {
TimeZoneCell(timezone: timezone)
Divider()
}
}
}
}
}
}.onAppear() {
value.scrollTo(TimeZone.current.identifier, anchor: .center)
}
}
}.navigationBarTitle(Text("Time Zones"), displayMode: .inline)
}
}


This following code is using a List, so the highlight is working as expected, but I cannot scroll to a specific item.

Code Block Swift
struct ContentView: View {
let timezones = TimeZone.knownTimeZoneIdentifiers.map { TimeZone(identifier: $0)! }
var body: some View {
let continents = Set(timezones.compactMap { $0.continent }).sorted()
List {
ForEach(continents, id: \.self) { continent in
let continentTZ = timezones.filter { $0.continent == continent}
Section(header: HeaderView(title: continent)) {
ForEach(continentTZ) { timezone in
Button(action: {
}) {
TimeZoneCell(timezone: timezone)
}
}
}
}
}.navigationBarTitle(Text("Time Zones"), displayMode: .inline)
}
}


I don’t think ScrollViewReader works with Lists yet. Hopefully, this will come in a future release.
This was actually fixed in Xcode 12 Beta 3.
How do I scroll a List to a specific item?
 
 
Q