I'm facing a weird bug where a list item within a NavigationView appears below a gap. When I scroll, it resets to correct location.
Minimum reproducible example on Xcode 14.1, iOS 16.1.2:
import SwiftUI
@main
struct Test_2022_12_03_02App: App {
var body: some Scene {
WindowGroup {
ListView()
}
}
}
struct ListView: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailView()) {
Text("Hello")
}
}
.navigationViewStyle(.stack)
}
}
struct DetailView: View {
// If I remove the line below, the bug disappears
@Environment(\.presentationMode) var presentationMode
var members = [1]
var body: some View {
List {
ForEach(members, id:\.self) { member in
Text("World")
}
}
}
}
This bug happens consistently on my phone, but doesn't always happen in simulators. Weirdly my friend's phone also displayed expected behavior with same iOS version.
More notes:
- If I remove
@Environment(\.presentationMode) var presentationMode
the bug disappears. (I need this line for my full app, to be able to return to main screen from the navigation link) - The bug also disappears if in the DetailView, we don't use a List, i.e.
struct DetailView: View {
// If I remove the line below, the bug disappears
@Environment(\.presentationMode) var presentationMode
var members = [1]
var body: some View {
List {
// ForEach(members, id:\.self) { member in
Text("World")
// }
}
}
}
Two questions:
- Do you know what's happening? Am I missing something in my code?
- If this is a freak bug, how do I find a workaround? Note that I need both the list and the ability to return to main screen in my full app.
Thanks!