【发布时间】:2020-08-14 06:50:02
【问题描述】:
我正在尝试在 SwiftUI 中创建这样的视图(抱歉它太大了):
具体来说,我正在尝试在屏幕的上四分之一处构建标签/条形图条的滚动行。对我来说,它看起来像一个水平滚动的 ScrollView,包含一些 X 个 barGraphItems,每个都是一个带有标签和彩色矩形的 VStack。然后将 VStack 旋转 270 度。
这是我目前的代码:
import SwiftUI
struct ContentView: View {
var body: some View {
HStack(alignment: .bottom, spacing: 0) {
Spacer()
VStack(alignment: .leading, spacing: 0) {
Text("Demo")
.font(.caption)
.fontWeight(.bold)
Rectangle().foregroundColor(.orange)
.frame(width: 84, height: 10)
}
.rotationEffect(Angle(degrees: 270.0))
VStack(alignment: .leading, spacing: 0) {
Text("Demo")
.font(.caption)
.fontWeight(.bold)
Rectangle().foregroundColor(.orange)
.frame(width: 84, height: 10)
}
.rotationEffect(Angle(degrees: 270.0))
VStack(alignment: .leading, spacing: 0) {
Text("Demo")
.font(.caption)
.fontWeight(.bold)
Rectangle().foregroundColor(.orange)
.frame(width: 84, height: 10)
}
.rotationEffect(Angle(degrees: 270.0))
VStack(alignment: .leading, spacing: 0) {
Text("Demo")
.font(.caption)
.fontWeight(.bold)
Rectangle().foregroundColor(.orange)
.frame(width: 84, height: 10)
}
.rotationEffect(Angle(degrees: 270.0))
VStack(alignment: .leading, spacing: 0) {
Text("Demo")
.font(.caption)
.fontWeight(.bold)
Rectangle().foregroundColor(.orange)
.frame(width: 84, height: 10)
}
.rotationEffect(Angle(degrees: 270.0))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
如何让 barGraphItems 靠得更近? 如何添加“预算”行? 如何在不滚动预算行的情况下使整个业务可滚动?
我认为屏幕上的其他元素我可以猜出来,但我整个下午都在摆弄这个条形图小部件,我被难住了。
【问题讨论】:
-
抱歉,忘了提及 - 每当我尝试向 HStack 添加更多 barGraphItems 时,它们会从屏幕上跑出并变得未对齐。右边的最后一个总是从其他的向上偏移。