gpt4 book ai didi

swiftui - 当按钮文本也有图像时如何居中?

转载 作者:行者123 更新时间:2023-12-02 02:13:02 39 4
gpt4 key购买 nike

在下面的代码中,我试图生成一个布局,使按钮文本居中,同时在右侧有一个图像。目前,文本偏移了图像的宽度而不是居中(我在标签中添加了一个 . 以便您可以看到它没有居中。我希望 . 位于按钮文本中的 B 之上)。

struct ContentView: View {
var body: some View {
VStack {
Text(".") // Center point
Button(action: {}, label: {
HStack {
Spacer()
Text("ABC")
Spacer()
Image(systemName: "person.circle.fill")
}
})
}
}
}

enter image description here

最佳答案

一种可能的方法是使用覆盖 - 默认情况下文本位于中心,但图像可以放在需要的地方。

注意:当然,如果您的文本是动态的并且可能很长,则需要一些更复杂的解决方案,但预期可能会有所不同

所以,对于你的快照

demo

Button(action: {}, label: {
Text("ABC")
.frame(maxWidth: .infinity)
.overlay(Image(systemName: "person.circle.fill").padding(),
alignment: .trailing)
})

关于swiftui - 当按钮文本也有图像时如何居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67490529/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com