如何将固定宽度 space 添加到 HStack 的开头?
How can I add fixed-width space to beginning of HStack?
在 SwiftUI 中,我将按钮添加到 HStack。我想要在 HStack 的开头有一个固定宽度的 space。目前这些按钮正对着屏幕的左边缘,我想要第一个按钮之前的 space 为 64px。我似乎找不到答案,我的 Google-Fu 可能让我失望了。
这是我的:
struct MyView: View {
var body: some View {
HStack(alignment: .center, spacing: 12, content: {
Button(action: doThis) {
Image("this").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Button(action: doThat) {
Image("that").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Spacer()
})
}
func doThis() {}
func doThat() {}
}
左边加点padding
就可以了
struct ContentView: View {
var body: some View {
HStack(alignment: .center, spacing: 12, content: {
Button(action: doThis) {
Image("this").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
.padding(.leading, 64) /// here!
Button(action: doThat) {
Image("that").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Spacer()
})
}
func doThis() {}
func doThat() {}
}
Before
After
在 SwiftUI 中,我将按钮添加到 HStack。我想要在 HStack 的开头有一个固定宽度的 space。目前这些按钮正对着屏幕的左边缘,我想要第一个按钮之前的 space 为 64px。我似乎找不到答案,我的 Google-Fu 可能让我失望了。
这是我的:
struct MyView: View {
var body: some View {
HStack(alignment: .center, spacing: 12, content: {
Button(action: doThis) {
Image("this").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Button(action: doThat) {
Image("that").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Spacer()
})
}
func doThis() {}
func doThat() {}
}
左边加点padding
就可以了
struct ContentView: View {
var body: some View {
HStack(alignment: .center, spacing: 12, content: {
Button(action: doThis) {
Image("this").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
.padding(.leading, 64) /// here!
Button(action: doThat) {
Image("that").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Spacer()
})
}
func doThis() {}
func doThat() {}
}
Before | After |
---|---|