SwiftUI中两个视图之间在HStack中的差距

时间:2019-10-25 06:26:34

标签: swift swiftui

我正在尝试使用SwiftUI在HStack中拥有两个视图。但是,我在两种观点之间一直保持差距。 纵向和横向布局都会出现此问题。

我的代码:

struct ContentView: View {

    var body: some View {

        GeometryReader { g in
            HStack {
                GeometryReader { g in
                    HStack {
                        VStack {
                            Text("View One")
                        }
                        .frame(width: g.size.width * 0.5, height: g.size.height, alignment: .center)
                        .background(Color.blue)

                        VStack {
                            Text("View Two")
                        }
                        .frame(width: g.size.width * 0.5, height: g.size.height, alignment: .center)
                        .background(Color.red)
                    }
                }
            }         
        }    
    }
}

The gap is between the View One and View Two

1 个答案:

答案 0 :(得分:7)

间距可以在初始化时设置

HStack(spacing: 0) {
    //Your code here
}