relativeSize不能调整HStack尺寸

时间:2019-06-14 18:00:48

标签: swiftui

在HStack {}上调用.relativeSize时,边界不会更近。指示尺寸未调整。

我已经尝试在HStack上重新排列方法调用的顺序,以便在.frame和.border之前和之后调用.relativeSize

struct ContentView : View {
    var body: some View{
        VStack(alignment: .center, spacing: 1){
            //Top box holding a rectangle
            HStack{
                Text("Test")
            }
            .frame(width: 355, height: 99, alignment: .center)
            .relativeSize(width: 0.95, height: 0.95)
            .border(Color.blue, width: 2)

            //Bottom box holding 3 recangles
            HStack{
                Text("Test")
            }
            .frame(width: 355, height: 47, alignment: .center)
            .border(Color.red, width: 1)
        }
        .frame(width: 355, height: 148, alignment: .center)
        .border(Color.black, width: 1)

    //body paren
    }

}

1 个答案:

答案 0 :(得分:0)

只需在文本前后添加一个Spacer(),然后删除.frame()修饰符即可。这样,HStack将横向扩展至包含VStack的大小的0.95。这就是你想要的吗?

struct ContentView : View {
    var body: some View{
        VStack(alignment: .center, spacing: 1){
            //Top box holding a rectangle
            HStack{
                Spacer()
                Text("Test")
                Spacer()
                }
                .relativeSize(width: 0.95, height: 0.95)
                .border(Color.blue, width: 2)

            //Bottom box holding 3 recangles
            HStack{
                Text("Test")

                }
                .frame(width: 355, height: 47, alignment: .center)
                .border(Color.red, width: 1)
            }
            .frame(width: 355, height: 148, alignment: .center)
            .border(Color.black, width: 1)

        //body paren
    }

}