SwiftUI List Selection On iPad
source link: https://useyourloaf.com/blog/swiftui-list-selection-on-ipad/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
SwiftUI List Selection On iPad
SwiftUI List Selection On iPad
There’s an annoying problem that causes SwiftUI lists to lose their selection when running on iPadOS.
What’s The Problem
Here’s my setup. My root view has a navigation view containing a project list and items belonging to the selected project:
struct ContentView: View {
var body: some View {
NavigationView {
ProjectList()
Text("Select a project")
}
}
}
In landscape, this gives us the classic iPad two-pane split view:
My project list and item list are similar. Here’s the ProjectList
view:
struct ProjectList: View {
@EnvironmentObject var store: ProjectStore
var body: some View {
List {
ForEach(store.projects) { project in
NavigationLink {
ItemList(project: project)
} label: {
ProjectRow(project: project)
}
}
}
.navigationTitle("Projects")
}
}
The problem comes when I have enough projects that the list can scroll. If you select a row near the bottom of the list and then scroll the row loses selection when it disappears offscreen (FB9961092):
This type of problem makes me wonder if I’m doing something wrong but the Apple sample code suffers from the same problem.
I assume this problem is also the reason why restoring the selection state requires you to scroll the selection on screen, see scrolling with ScrollViewReader.
There’s a related problem if you need navigation in the detail view. As soon as you push a view onto the detail navigation stack the selection in the main/sidebar list loses selection (FB9961758).
Workarounds?
I don’t have good workarounds for these issues other than falling back to UIKit. Are you using SwiftUI lists on iPadOS? What am I missing? Have you found a good workaround?
If you’d like Apple to fix this consider filing a feedback. You can find my feedback report, sample code and a screen recording of the problem at FB9961092 in my radar GitHub repository.
Never Miss A Post
Sign up to get my iOS posts and news direct to your inbox and I'll also send you a PDF of my WWDC 2021 Viewing Guide
Unsubscribe at any time. See privacy policy.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK