SwiftUI Form space between two buttons

149 Views Asked by At

I have a form with 2 buttons. Now I would like to have a little space between these two buttons. Exactly like in this example: enter image description here

Does anyone know how to implement this? I appreciate any help!

1

There are 1 best solutions below

0
Trupesh Vaghasiya On

Try with my below code to achieve your exact output.

In the init method TableView sectionHeaderHeight is set to .zero

I have added one button to the section and added an empty title to set dummy space.

struct ContentView: View {
    
    init() {
        UITableView.appearance().sectionHeaderHeight = .zero
    }
    
    var body: some View {
        Form {
            Section {
                HStack {
                    Text("Zeit:")
                        .fontWeight(.semibold)
                    
                    Spacer()
                    
                    Text("Zeit:")
                        .fontWeight(.semibold)
                }
                
                DatePicker(selection: .constant(Date())) {
                    Text("Erstellt:")
                        .fontWeight(.semibold)
                }
            }
            
            Section(header: Text("")) {
                Button {
                } label: {
                    HStack {
                        Spacer()
                        
                        Text("Teilen")
                        
                        Spacer()
                    }
                }
            }
            
            Button {
                
            } label: {
                HStack {
                    Spacer()
                    
                    Text("Löschen")
                    
                    Spacer()
                }
            }
            .tint(.red)
        }
    }
}

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

Output:

enter image description here

Replace the above code and try with that...!