我正在创建一个iOS扫雷游戏,想要在顶部放一个栏,包含以下三个信息:
- 最高分(旁边是一个奖杯符号)[左侧]
- 当前时间(旁边是一个计时器符号)[中间]
- 剩余炸弹数量(旁边是一个炸弹符号)[右侧]
然而,这些元素的对齐并不平均,就像您可以在底部的图片中看到的那样-我猜测空格自动具有相同的大小。因此,由于屏幕左侧的文本比右侧的文本更宽,所以中间的文本偏移向右。
我如何对齐这些项目,使得中心的时间/图像与左/右侧的其他对象完全居中对齐?
我的代码如下:
struct GameView: View {
@EnvironmentObject var game: Game
@State var labelHeight = CGFloat.zero
var body: some View {
VStack(alignment: .center, spacing: 10) {
Text("MINESWEEPER")
.font(.system(size: 25, weight: .bold, design: .monospaced))
.kerning(3)
.foregroundColor(.red)
HStack(alignment: .center, spacing: 5) {
Image(systemName: "rosette")
.resizable()
.aspectRatio(contentMode: .fit)
.foregroundColor(.black)
Text(game.highScoreString)
.font(.system(size: 15, weight: .bold, design: .monospaced))
.foregroundColor(.black)
Spacer()
Image(systemName: "timer")
.resizable()
.aspectRatio(contentMode: .fit)
.foregroundColor(.black)
Text(game.timerString)
.font(.system(size: 15, weight: .bold, design: .monospaced))
.foregroundColor(.black)
Spacer()
Image("top_bomb")
.resizable()
.aspectRatio(contentMode: .fit)
Text(String(game.bombsRemaining))
.font(.system(size: 15, weight: .bold, design: .monospaced))
.foregroundColor(.black)
.overlay(GeometryReader(content: { geometry in
Color.clear
.onAppear(perform: {self.labelHeight = geometry.frame(in: .local).size.height})
}))
}
.frame(width: UIScreen.main.bounds.width * 0.9, height: labelHeight, alignment: .center)
BoardView()
}
}
}