使用SwiftUI在类似于列表的VStack中等间距的标签

m4pnthwp  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(263)

如何使VStack中标签中的图像像List中的图像一样间距相等?
这里有一个例子。我想在标签中的每个图像采取相同的宽度间距,因为他们将在一个列表。

struct ContentView: View {
    var body: some View {
        VStack(alignment: .leading) {
            Label("Hello, world!", systemImage: "globe")
            Label("Hello, world!", systemImage: "bubble.left.and.bubble.right.fill")
            Label("Hello, world!", systemImage: "bubble.right")
        }
        .padding()
    }
}
ikfrs5lh

ikfrs5lh1#

您可以制作自己的LabelStyle,并确定标签的确切外观。

struct MyStyle: LabelStyle {
    func makeBody(configuration: Configuration) -> some View {
        HStack {
            configuration.icon
                // fix your width here
                // width could also be passed as a initialiser parameter
                .frame(width: 50)
            configuration.title
        }
    }
}

使用方法:

VStack(alignment: .leading) {
    Label("Hello, world!", systemImage: "globe")
    Label("Hello, world!", systemImage: "bubble.left.and.bubble.right.fill")
    Label("Hello, world!", systemImage: "bubble.right")
}
.labelStyle(MyStyle())

相关问题