How to remove right left padding of `Form` in SwiftUI?

1.8k views Asked by At

How to remove the left and right Padding of a Form in SwiftUI? Every View I create has padding in the leading and trailing.

enter image description here

Here is my code

init() {
            UITableView.appearance().tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: Double.leastNonzeroMagnitude))
        }
    
    var body: some View {
        NavigationView{
            Form{
                Section{
                    Picker(selection: $selectedCurrency, label: Text("Date Format"), content: {
                        ForEach(0 ..< self.dateStyles.count) { (index:Int) in
                            Text("\(self.dateStyles[index])")
                           
                        }
                    })
                }
            }.listStyle(PlainListStyle())
            .listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
            .navigationBarTitle(Text("Settings"), displayMode: .inline)
            .onAppear(){
                if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] != "1"{
                //some code
                }
        }
            
        }
    }
2

There are 2 answers

1
lorem ipsum On

Using something else is probably best but this removes the padding.

Form {
 ...               
}.padding(.leading, -16)
.padding(.trailing, -16)
0
DKvarn On

You should use a List instead of a Form, take a look at the different list styles, I think you are after .listStyle(GroupedListStyle())