在两个不同的堆栈中等于垫片的高度

时间:2020-03-18 09:07:05

标签: swift stack swiftui

例如,我有以下视图:

import SwiftUI

struct TarifsScreen: View {

var body: some View {

    GeometryReader { geometry in

        VStack {

            VStack() {

                Spacer()

                Text("Text1")

                Spacer()

                Text("Text2")

                Spacer()

                Text("Text3")

                Spacer()

                Text("Text4")

                Spacer()

                VStack() {

                    Spacer()

                    Text("Text5")

                    Spacer()

                    Text("Text6")

                    Spacer()

                    Text("Text7")

                    Spacer()

                    Text("Text8")

                    Spacer()
                }
            }

        }.frame(width: geometry.size.width, height: geometry.size.height)
    }
}

}

较低堆栈中的垫片很大,但是较高堆栈中的垫片很小。如何使所有垫片的高度相等?我不能对所有元素仅使用一个堆栈,因为我总共有10个以上的元素

1 个答案:

答案 0 :(得分:1)

如果问题仅出在10个以上的子视图中,则Group的使用会更加简单

var body: some View {

    GeometryReader { geometry in
        VStack {
            Group {
                Spacer()
                Text("Text1")
                ...
            }
            Group {
                Spacer()
                Text("Text5")
                ...
            }
            ...

如果您有相同的常规模式值得考虑ForEach(它会自动进行分组),例如

var body: some View {

    GeometryReader { geometry in
        VStack {
            ForEach(0 ..< count) { index in
                Spacer()
                Text("Text\(index)")
            }
        }