Swiftui 如何以编程方式调整图像和文本之间的间距? [英] Swiftui how to programatically adjust spacing between images and text?
问题描述
我正在尝试以编程方式在 swiftui 中获得对齐的文本-图像对.可能吗?
I'm trying to get an aligned text-image pairs in swiftui programmatically. Is it possible?
我希望访问"这个词在两行的相同位置开始.
I want the word "Visit" to start from the same position in both rows.
这是我的代码:
HStack{
Image(systemName: item.leftImage) // A system image name
Text(item.text) // A text representing the item
}
我知道我可以为 HStack 本身添加间距,但它不会解决我的问题.由于苹果提供的每个系统图像都有不同的大小,我想我应该以编程方式确定每个图像与文本之间应该实例化的间距.
I know that I can add spacing to the HStack itself, but it won't fix my issue. Since every systemimage provided by apple has a different sizing, I guess that I should programmatically somehow determine the spacing that should be instantiated between each the image to the text.
我该怎么做?
推荐答案
我建议为图像框架使用显式宽度,这将自动适合任何符号并使其居中
I would recommend to use explicit width for image frame, this will fit any symbol automatically and make it centred
HStack{
Image(systemName: item.leftImage)
.frame(width: 28)
Text(item.text)
}
这篇关于Swiftui 如何以编程方式调整图像和文本之间的间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!