struct ContentView: View {
var body: some View {
VStack(spacing: 0) {
HStack {
Image(systemName: "circle")
.foregroundColor(.red)
.frame(width: 30, height: 30)
Text("Left")
}
.background(Color.gray)
HStack {
Text("Middle")
.frame(maxWidth: .infinity)
}
.background(Color.green)
HStack {
Text("Right")
Image(systemName: "circle")
.foregroundColor(.red)
.frame(width: 30, height: 30)
}
.background(Color.gray)
}
}
}
在上面的示例代码中,我们使用了VStack来包含三个不同的HStack,每个HStack都有不同的颜色作为背景。在最中间的HStack中,我们使用.maxWidth可以使其占用剩余的可用空间,从而以流体方式适应不同的设备宽度。
此外,我们还使用了.frame来设置每个图像的大小,这可以保持图像的比例并使其在不同设备上保持一致。
下一篇:不理解if语句