flymg
flymg

Reputation: 637

SwiftUI List Rows fill width

In SwiftUI 2.0, i have a List with some text (MacOS App)

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)
}

This leads to the following output in preview:

enter image description here

How can i make the rows fill all available space? (Blue Frame of List should match blue background of text)

Adding a .listStyle(PlainListStyle()) to the List element, reduces the space, but not enough:

enter image description here

Upvotes: 3

Views: 2289

Answers (2)

Bill Haku
Bill Haku

Reputation: 281

You can try with this:

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

Upvotes: 11

flymg
flymg

Reputation: 637

It seems the only solution to this at the moment is adding the ugly workaround

.padding(.horizontal, -15.0)

to the List(...)

Upvotes: 2

Related Questions