Search code examples
swiftuiuinavigationbar

navigationBarHidden combined with inline display mode causes jump


I have a parent view, in which I don't want any navigation bar, and a child view, where I want an inline navigation bar.

If I navigate to the child view, then back again. The top of the list will have a weird jump effect when scrolling upwards.

I'm sure this is a bug, but does anyone have a workaround? If it helps, I can get access to the underlying UIScrollView/UINavigationController components - but I'm not sure if any of the properties would help.

Screen recording of the bug

struct ContentView: View {
    var body: some View {
        NavigationView {
            List( 0...50, id: \.self ) { i in
                NavigationLink(destination: HelloView()) {
                    Text("\(i)")
                }
            }
            .navigationBarHidden( true )
        }
    }
}

struct HelloView: View {
    var body: some View {
        Text("Hello")
            .navigationBarTitle("Hello", displayMode: .inline)
    }
}

Solution

  • I realize this is odd, but this can be alleviated by setting the navigationBarTitle property. In your desired case I would recommend the following:

    struct ContentView: View {
        var body: some View {
            NavigationView {
                List( 0...50, id: \.self ) { i in
                    NavigationLink(destination: HelloView()) {
                        Text("\(i)")
                    }
                }
                .navigationBarTitle("", displayMode: .inline) /// <<--- Insert this line
                .navigationBarHidden( true )
            }
        }
    }
    

    By setting the title attribute to blank and using the inline display mode, it rids the view of the large title and actually hides the view correctly.