简体   繁体   中英

Remove default padding from List in SwiftUI

When using ScrollView the views inside it are spread across the whole screen width by default, but when using List, there is a padding on the sides. Is there a way to get rid of this padding?

To achieve this you need to use ForEach inside List combined with .listRowInsets as in example below

演示

struct Demo: View {
    var colors: [Color] = [.red, .blue, .yellow]
    var body: some View {
        List {
            ForEach(colors, id: \.self) { color in
                color
            }.listRowInsets(EdgeInsets())
        }
    }
}

For iOS 15.0+ , you may try listStyle modifier and set it as plain .

var body: some View {
        List {
            // some rows
        }
        .listStyle(.plain)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM