为什么 SwiftUI“Form”导致我的按钮转到屏幕底部?

Cam*_*ige 4 swiftui swiftui-form

我想创建一个视图,该视图有一个表单,其下方有一个按钮。如果我包含一个表单和一个按钮,该按钮将显示在屏幕底部。

没有表单元素 在此输入图像描述

带有表单元素 在此输入图像描述

这只是 SwiftUI 的一个 bug 吗?或者我做错了什么?

//
//  TestFile.swift
//  searchparty
//
//  Created by Me
//

import SwiftUI

struct TestFile: View {
    var body: some View {
        VStack {
            Form{
                Text("Hello, World!")
            }
            
            Button("Button") {
                print("Button tapped!")
            }
        }
    }
}

struct TestFile_Previews: PreviewProvider {
    static var previews: some View {
        TestFile()
    }
}
Run Code Online (Sandbox Code Playgroud)

jn_*_*pdx 10

Section带有 a 的空footer将完成这项工作,尽管您需要显式设置字体,否则将footer根据表单页脚环境更改它:

struct ContentView: View {
    var body: some View {
        Form {
            Text("Entry")
            
            Section(footer:
                        HStack {
                            Spacer()
                            Button(action: {}) {
                                Text("My button")
                                    .font(.system(.body))
                            }
                            Spacer()
                        }
            ) {
                EmptyView()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)