如何动态收紧 TabView 的高度?

How to dynamically tighten height for TabView?

TabView 似乎伸展到了最大高度,但我希望它尽可能缩短到所需的高度,而不是空的 space。这就是它的样子,即使周围有 Spacer 个视图:

struct ContentView: View {
    var body: some View {
        Text("Some text")
        Text("Some text")
        Text("Some text")
        Spacer()
        TabView {
            GroupBox(label: Text("This is the title 1")) {
                Label("Some item 1", systemImage: "checkmark.circle.fill")
                Label("Some item 2", systemImage: "checkmark.circle.fill")
                Label("Some item 3", systemImage: "checkmark.circle.fill")
                Label("Some item 4", systemImage: "checkmark.circle.fill")
            }
            .padding()
            GroupBox(label: Text("This is the title 2")) {
                Label("Some item 1", systemImage: "checkmark.circle.fill")
                Label("Some item 2", systemImage: "checkmark.circle.fill")
                Label("Some item 3", systemImage: "checkmark.circle.fill")
                Label("Some item 4", systemImage: "checkmark.circle.fill")
            }
            .padding()
            GroupBox(label: Text("This is the title 3")) {
                Label("Some item 1", systemImage: "checkmark.circle.fill")
                Label("Some item 2", systemImage: "checkmark.circle.fill")
                Label("Some item 3", systemImage: "checkmark.circle.fill")
                Label("Some item 4", systemImage: "checkmark.circle.fill")
            }
            .padding()
        }
        .tabViewStyle(PageTabViewStyle())
        .background(Color.yellow)
        Spacer()
        Text("Some text")
        Text("Some text")
        Text("Some text")
    }
}

有没有办法将高度动态地收紧到其子项需要的高度?

使用.frame( height: .leastNormalMagnitude)

.tabViewStyle(PageTabViewStyle())
.background(Color.yellow)
.frame( height: .leastNormalMagnitude)

页面索引涵盖了浏览量。您可能需要为 GroupBoxes 提供 40-50 点的底部填充或更改 indexViewStyle 的背景。

 .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))