如何去掉 SwiftUI Form 上面的 space 部分?

How to eliminate the space above Section in SwiftUI Form?

我想消除Form

中第一个Section上方的space

 var body: some View {
        VStack {
            Text("Text 1")
            Form {
                Section {
                    Text("Text 2")
                }
            }
        }
    }

我尝试将Section的header的frame设置为0,但是没有用

解决方案是将 SectionEmptyView() 一起使用,然后将您希望位于顶部的视图置于此 Section 的 header

 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)
            }
        }
    }

这个要简单得多:

struct ContentView: View 
{

    init() 
    {
        UITableView.appearance().tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: Double.leastNonzeroMagnitude))
    }

    var body: some View 
    {

    }
}

我会提供一个虚拟 header 视图并将其大小设置为零:

Section(header: Color.clear
                .frame(width: 0, height: 0)
                .accessibilityHidden(true)) {
  Text("Text 2")
}

最佳解决方案可能取决于您的要求。对我有用的是一个空的文本视图,如:

  Section(header: Text("")) {
    MySectionView()
  }

顺便说一句,我尝试了 EmptyView() 但它被完全忽略了;如在 space 仍然存在。