7

I am trying to display a row inside my ListView in SwiftUI for MacOS. However, my issue is that the row contains padding around it. I would like to stretch it to the boundaries of my ListView.

Apple uses .listRowInsets(EdgeInsets()) in their example. However, that is only shown on iOS. For me, it is not working on macOS.

My row got a red border to visualize the issue. I want it to be stretched all the way to the boundaries of the List Row, so fill the whole blue row. Is that possible in macOS?

Thanks in advance.

preview

2
  • Use a custom XIB/NIB file. It's much easier to manage that. then remove the separator lines. Commented Mar 17, 2020 at 14:40
  • show us the simplified code of your "ListView" Commented Mar 17, 2020 at 15:00

2 Answers 2

12

For now I've found only workaround (.listRowInsets should really do this work, so worth submitting feedback to Apple):

demo

struct TestListRow: View {
    var body: some View {
        List {
            ForEach (0..<3) { i in
                HStack {
                    Text("Test row \(i)").font(.largeTitle)
                    Spacer()
                }
            }
            .listRowBackground(Color.green)
            .border(Color.red)
            .padding(.horizontal, -8)   // << workaround !!
        }.environment(\.defaultMinListRowHeight, 40)
        .border(Color.yellow)
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Working great .. where to report these bugs? @Asperi
This removes the spacing, but messes up the body completely. Overriding the defaultMinListRowHeight doesn't seem to be working nicely for me.
Hmm defaultMinListRowHeight and the insets seemed to fix the problem for me
It didn't work for me. Have you faced more issues ? I'm using the Xcode beta imgur.com/gallery/lJqY69f
0

I added this code at end of the row and the padding was shrunk!!!

    Spacer()
    Text(" ")
    .padding()

Sample Code

import SwiftUI


struct testViews1: View {
    var body: some View {
        List {
            ForEach (0..<3) { i in
                HStack {
                    Text("row \(i)")
                    Spacer()
                }
                .background(Color.red)
                .frame(height: 30)  
            }
        }
    }
}


struct testViews2: View {
    var body: some View {
        List {
            ForEach (0..<3) { i in
                HStack {
                    Text("row \(i)")
                    Spacer()
                    Text(" ")
                        .padding()   
                }
                .background(Color.red)
                .frame(height: 30)
                
            }
        }
    }
}

struct testViews_Previews: PreviewProvider {
    static var previews: some View {
        testViews1()
            .frame(height: 150)
        testViews2()
            .frame(height: 150)

    }
}

output enter image description here

It works with SwiftUI 1.

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.