Anton
Anton

Reputation: 3267

Navigating to List View in SwiftUI immediately shifts the list upward offscreen

A simple NavigationLink to a List view gives me the following strange behavior. After the navigation successfully occurs, the list abruptly shifts to where the title and back button are no longer on the screen:

Screen capture of strange behavior

Here is the code (Xcode 11.5). Any idea what's going on?

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: ListView()) {
                Text("Go to List View ›")
            }.navigationBarTitle("").navigationBarHidden(true)
        }
    }
}

struct ListView: View {
    var body: some View {
        List {
            ForEach(0 ..< 3) { person in
                HStack {
                    Image(systemName: "person.crop.circle.fill")
                        .resizable().frame(width: 80, height: 80)
                    Text("User \(person+1)")
                }
            }
        }.listStyle(GroupedListStyle())
            .navigationBarTitle("List")
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Upvotes: 0

Views: 880

Answers (1)

pawello2222
pawello2222

Reputation: 54516

The issue occurs because you try to hide/show the NavigationBar in a way which is not currently supported in SwiftUI (it may be fixed in the next releases).

This bug in SwiftUI occurs when you try to make the NavigationBar hidden in the root view and shown in the detail view:

EDIT

It seems like this bug was fixed in Xcode 12 beta.

Upvotes: 1

Related Questions