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

问题描述 投票:1回答:1

我正在尝试使用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

swift swiftui
1个回答
7
投票

可以在初始化时设置间距

HStack(spacing: 0) {
    //Your code here
}
© www.soinside.com 2019 - 2024. All rights reserved.