1

I'm currently using a List with a GroupedListStyle(), and with a .regular horizontalSizeClass (e.g an iPad in landscape mode), this style of List automatically creates some padding to each section, like this:

struct ListView: View {
    var body: some View {
        List {
            Section {
                Text("One")
                Text("Two")
                Text("Three")
            }
        }.listStyle(GroupedListStyle())
    }
}

Sample Grouped List Style

This is great, I want this. But since I need to display some elements inside the rows with a fixed width, I need to know the actual width of the row inside the List.

I've tried wrapping the List with a GeometryReader, but the width doesn't match the row's width because of the padding around the entire Section.

struct ListView: View {
    var body: some View {
        GeometryReader { geometry in
             List {
                Section {
                    Text("One")  // <-- geometry.size.width != Text.width
                    Text("Two")
                    Text("Three")
                }
             }.listStyle(GroupedListStyle())
        }
    }
}

So I've tried using the PreferenceKey method explained here by putting a GeometryReader in the listRowBackground modifier of the row, but the value is never updated.

Is there a working way to get the size of the parent, without using a GeometryReader around the parent (as it is finnicky on a List, and needs to be specified Height and Width to work properly)?

riciloma
  • 1,456
  • 2
  • 16
  • 31
  • What exactly do you need to know the height of? Section or row element? – Valentin Jun 10 '20 at 00:13
  • Try the `defaultMinListRowHeight` Environment value like this: `@Environment(\.defaultMinListRowHeight) var height` – Valentin Jun 10 '20 at 00:17
  • So do you want to read row's width or text's width? Because those are different widths. Row's width is equal for all rows in table. Text's width depend on content, of course. – Asperi Jun 10 '20 at 04:02
  • I need to read the row's width. – riciloma Jun 10 '20 at 06:20

1 Answers1

0

I've had to resort to a UITableView with style .insetGrouped, implemented with a Representable and used the property layoutMargins that gives exactly the size of the horizontal margins in this size class.

riciloma
  • 1,456
  • 2
  • 16
  • 31