lvollmer
lvollmer

Reputation: 1618

SwiftUI macOS List Row removing padding

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

Upvotes: 7

Views: 2617

Answers (2)

Asperi
Asperi

Reputation: 258385

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

Upvotes: 13

Andreas Stokidis
Andreas Stokidis

Reputation: 402

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.

Upvotes: 0

Related Questions