SwiftUI 列表行填充宽度

fly*_*ymg 2 macos list swiftui

在 SwiftUI 2.0 中,我有一个包含一些文本的列表(MacOS 应用程序)

typealias Row = String

@State var row: [Row] = ["1 Row with some content", "2 Some content", "3 Another Row"]

List(row, id: \.self) { content in
    Text(content)
        .truncationMode(.middle)
        .lineLimit(1)
        .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
        .background(Color.blue)
}
Run Code Online (Sandbox Code Playgroud)

这会导致预览中出现以下输出:

在此输入图像描述

如何使行填满所有可用空间?(列表的蓝色框应与文本的蓝色背景相匹配)

在 List 元素中添加 a.listStyle(PlainListStyle())可以减少空间,但还不够:

在此输入图像描述

小智 6

你可以尝试用这个:

    Text(content)
        .listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
        .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
        .listRowBackground(Color.white.opacity(0))
Run Code Online (Sandbox Code Playgroud)