4

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

2
  • Does this answer your question stackoverflow.com/a/60727518/12299030? Commented Dec 24, 2021 at 17:00
  • has the same topic, but the solutions there do not work Commented Dec 26, 2021 at 10:24

2 Answers 2

12

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))
Sign up to request clarification or add additional context in comments.

2 Comments

.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0)) did the trick
.listRowInsets(EdgeInsets()) looks better
2

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

.padding(.horizontal, -15.0)

to the List(...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.