Reputation: 63
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:
@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)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:
Thanks!
Upvotes: 0
Views: 266
Reputation: 1
NavigationView is deprecated and so is presentationMode. There is bound to be unforeseen bugs using deprecated methods. Use navigation stack instead: https://developer.apple.com/documentation/swiftui/navigationstack
Upvotes: -1