0

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.

Screen Capture

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!

Tom Petit
  • 33
  • 5
  • 1
    I just started noticing this too, after installing 16.1.2. Similarly appears to be affected if you navigated from a List or not. – tiritea Dec 07 '22 at 01:01

1 Answers1

-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