Search code examples
iosxcode12swiftui

Unexpected Padding Swift UI List in NavigationView


Simple list embedded in navigation view renders as expected:

NavigationView {
        List {
            HStack {
                Text("Row 1")
            }
            HStack {
                Text("Row 2")
            }
            HStack {
                Text("Row 3")
            }
        }
    }

enter image description here

But as soon as I add navigation bar button, I get unexpected paddings around the List.

NavigationView {
        List {
            HStack {
                Text("Row 1")
            }
            HStack {
                Text("Row 2")
            }
            HStack {
                Text("Row 3")
            }
        }
        .navigationBarItems(leading: Button("Add") {}) 
    }

enter image description here

Where are those paddings are coming from and how to get rid of them?


Solution

  • That is the default listStyle for that combination. You should explicitly set that to plain:

    .listStyle(PlainListStyle())