0

似乎伸展到最大高度,但我希望它缩短到所需的TabView高度,而不是空白。这就是它的样子,即使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")
    }
}

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

4

1 回答 1

1

利用.frame( height: .leastNormalMagnitude)

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

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

 .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
于 2021-03-06T18:28:34.633 回答