Home > Blockchain >  How can I made a SwiftUI List full width?
How can I made a SwiftUI List full width?

Time:10-13

I want my List to be full width. Padding is being added automatically and for my purpose I don't want that. This is just sample code.

This is my code:

struct ViewModel: Identifiable {
    let title: String
    let id = UUID()
}

struct ContentView: View {
    
    let models = [
        ViewModel(title: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
        ]
    
    var body: some View {
        List(models, id: \.id) { model in
            Text(model.title)
                .frame( maxWidth: .infinity)
        }.frame( maxWidth: .infinity).edgesIgnoringSafeArea(.all)
    }
}

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

enter image description here

CodePudding user response:

The list style you see is known as InsetGroupedListStyle. Note the word "inset". You can add a .listStyle modifier to change it to a style that has no insets:

List(models, id: \.id) { model in
    Text(model.title)
        .frame( maxWidth: .infinity)
}
.frame( maxWidth: .infinity)
.edgesIgnoringSafeArea(.all)
.listStyle(GroupedListStyle()) // or PlainListStyle()
  • Related