SwiftUI 中按钮的位置
Location of buttons in SwiftUI
我正在制作一个应用程序但遇到问题,我的界面(按钮、图像等)位于屏幕中央。我想将它们放在顶部。这是代码
var body: some View {
VStack{
HStack{
Button {
} label: {
Image(systemName: "pencil")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 45, height: 45)
.clipShape(Circle())
}
.hLeading()
Button {
} label: {
Image(systemName: "calendar")
.font(.title2)
.foregroundColor(.white)
}
Button {
} label: {
Image(systemName: "bell")
.font(.title2)
.foregroundColor(.white)
}
}
.padding()
}
.vTop()
.hCenter()
.background(Color("BG"))
.preferredColorScheme(.dark)
}
有人可以帮我吗?
在您的 VStack 中添加 Spacer()
像这样:
var body: some View {
VStack{
HStack{
Button {
} label: {
Image(systemName: "pencil")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 45, height: 45)
.clipShape(Circle())
}
.hLeading()
Button {
} label: {
Image(systemName: "calendar")
.font(.title2)
.foregroundColor(.white)
}
Button {
} label: {
Image(systemName: "bell")
.font(.title2)
.foregroundColor(.white)
}
}
.padding()
Spacer() // <- this
}
.vTop()
.hCenter()
.background(Color("BG"))
.preferredColorScheme(.dark)
}
我正在制作一个应用程序但遇到问题,我的界面(按钮、图像等)位于屏幕中央。我想将它们放在顶部。这是代码
var body: some View {
VStack{
HStack{
Button {
} label: {
Image(systemName: "pencil")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 45, height: 45)
.clipShape(Circle())
}
.hLeading()
Button {
} label: {
Image(systemName: "calendar")
.font(.title2)
.foregroundColor(.white)
}
Button {
} label: {
Image(systemName: "bell")
.font(.title2)
.foregroundColor(.white)
}
}
.padding()
}
.vTop()
.hCenter()
.background(Color("BG"))
.preferredColorScheme(.dark)
}
有人可以帮我吗?
在您的 VStack 中添加 Spacer() 像这样:
var body: some View {
VStack{
HStack{
Button {
} label: {
Image(systemName: "pencil")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 45, height: 45)
.clipShape(Circle())
}
.hLeading()
Button {
} label: {
Image(systemName: "calendar")
.font(.title2)
.foregroundColor(.white)
}
Button {
} label: {
Image(systemName: "bell")
.font(.title2)
.foregroundColor(.white)
}
}
.padding()
Spacer() // <- this
}
.vTop()
.hCenter()
.background(Color("BG"))
.preferredColorScheme(.dark)
}