How to eliminate the space above Section in SwiftUI Form?
The solution is to use a Section
with an EmptyView()
and place the view you want to be at the top in the header of this Section
var body: some View {
VStack {
Text("Text 1")
Form {
Section(header: VStack(alignment: .center, spacing: 0) {
Text("Text 2").padding(.all, 16)
.frame(width: UIScreen.main.bounds.width, alignment: .leading)
.background(Color.white)
}) {
EmptyView()
}.padding([.top], -6)
}
}
}
I'd supply a dummy header view and set its size to be zero:
Section(header: Color.clear
.frame(width: 0, height: 0)
.accessibilityHidden(true)) {
Text("Text 2")
}
The best solution may depend on your requirements. What worked for me was an empty Text view as in:
Section(header: Text("")) {
MySectionView()
}
BTW, I tried EmptyView() but that is ignored completely; as in the space still remains.