【发布时间】:2021-12-18 04:36:42
【问题描述】:
我尝试让键盘上的按钮在水平方向上靠得更近。首先我尝试调整按钮框架的宽度。但是我发现如果我减小框架宽度,一些像“W”这样的长宽字符将无法正确显示。
然后我尝试将 HStack 的间距设置为负值,就像我在下面的代码中所做的那样。
但这会导致按钮相互重叠,可点击区域会向左移动,这是不可接受的(可以通过将背景颜色设置为蓝色来检查)。
有没有办法在不改变字体大小的情况下减少按钮距离?
struct KeyboardView: View {
let KeyboardStack = [["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
["A", "S", "D", "F", "G", "H", "J", "K", "L"],
["Z", "X", "C", "V", "B", "N", "M"]]
var body: some View {
VStack(spacing: 9) {
ForEach(KeyboardStack.indices) { row in
let num = KeyboardStack[row].indices
HStack(spacing: -12) {
ForEach(num) { column in
Button(KeyboardStack[row][column]) {}
.frame(width: 12, height: 12,alignment: .center)
.padding()
// .background(Color.blue)
.font(.system(size: 15, weight: .regular, design: .default))
.foregroundColor(.white)
.buttonStyle(PlainButtonStyle())
}
}
.frame(width: 255, height: 19.5, alignment:.center)
}
}
.frame(width: 445, height: 60, alignment:.center)
}
}
【问题讨论】: