我想创建一个顶栏,我的代码如下所示:
struct MyView: View {
var body: some View {
VStack(alignment: .leading) {
Bar()
Spacer()
}
}
}
struct Bar: View {
var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack {
Spacer()
Text("Community").comfortaa(weight: .bold, size: 20).foregroundColor(.therameBlack)
Spacer()
Button(action: {
}) {
Image(systemName: "gearshape.fill").font(.headline).foregroundColor(.therameBlack)
}
}
}.padding(.leading,5).padding(.trailing,5)
.padding(.top,(UIApplication.shared.windows.last?.safeAreaInsets.top)! + 10)
}
}
我也尝试过这样做,正如这篇文章How to move vstack at the top in swiftui?
struct MyView: View {
var body: some View {
VStack {
VStack(alignment: .leading) {
TopBar()
Spacer()
}
Spacer()
}
}
}
但它没有帮助......另外,如果我从 .safeAreaInsets 中删除 +10,顶部仍有空间。