Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI view content layout unexpectedly pop / jumps on appear?

Tags:

ios

swift

swiftui

When navigating to a new view with a form, or list (grouped), the content appears about a quarter of the way down from the bottom of the navigation view title, then "pops" up to the correct location about a half second later. Very ugly. Not sure if this is a SwiftUI bug or i'm structuring something incorrectly?

I have tested this on both the simulator and device with same results.

Im thinking it has something to do with the way navigation links are handled? Every navigation link calls the init() on its destination view when ContentView appears. To me this makes no sense, shouldnt the destination init() only be called the moment a user actually clicks the navigation link?

I have tried this with a sheet as well and the problem does not exist with sheets, only when using navigation links with forms or lists styled as grouped.

Code from a brand new single view application:

struct ContentView: View {

    var body: some View {
        NavigationView {
            NavigationLink(destination: TestView()) {
                Text("Test Link")
            }
        .navigationBarTitle("Content View")
        }
    }

}

struct TestView: View {

    var body: some View {
        Form {
            Text("Test View")
        }
        .navigationBarTitle("Test View")
    }

}
like image 902
Steve Da Monsta Avatar asked Nov 07 '19 21:11

Steve Da Monsta


1 Answers

Providing a section with a header text fixes the issue, even if the text is blank. Although this will leave a gap between the navigation title and first row.

Works with both Forms & Lists styled as grouped.

struct TestView: View {

    var body: some View {
        Form {
            Section(header: Text("")) {
                Text("Test View")
            }
        }
        .nvigationBarTitle("Test View")
    }
}
like image 152
Steve Da Monsta Avatar answered Nov 19 '22 16:11

Steve Da Monsta