你没有给它任何对齐,所以它默认了。对于HStack,这意味着垂直居中。V定位默认为水平居中。添加适当的路线可以缓和视图。代码中的注释:
struct PanView: View {
@Binding var value: Double
var body: some View {
// bottom align this HStack
HStack(alignment: .bottom, spacing: 10) {
leftBar
rightBar
}
}
...
}
struct PanView_Used: View {
@State private var barValue:Double = 0.0
var body: some View {
VStack {
Text("Bar Graph at \(Int(barValue))")
.font(.title)
PanView(value: $barValue)
// Frame this since you know the max height already, and bottom align it.
.frame(height: 100, alignment: .bottom)
Slider(value: $barValue, in: -50...50, step:Double.Stride(1), onEditingChanged: {e in})
}
}
}
编辑:
在平移视图中的HStack上也可以。
struct PanView: View {
@Binding var value: Double
var body: some View {
// bottom align this HStack
HStack(alignment: .bottom, spacing: 10) {
leftBar
rightBar
}
// Frame this since you know the max height already, and bottom align it.
.frame(height: 100, alignment: .bottom)
}