8

For some reason, putting a GeometryReader as an intermediary, kills geometry of its nested views if it's a List "cell".

Example code:

struct SampleView: View {
    var multilineText: some View {
        Text(
            """
            Some
            Amazing
            Multiline
            Copy
            """
        )
    }

    var body: some View {
        List(1...5, id: \.self) { _ in
            GeometryReader { _ in
                self.multilineText
            }
        }
    }
}

Without GeometryReader (Expected) / Actual with GeometryReader:

This example, obviously, is over-simplified, but there is a legitimate reason to measure geometry for a nested view I'm building.

This is on Xcode 11 beta 6. Should I go straight into reporting this as a bug, or it's something expected and workable around?

rshev
  • 4,086
  • 1
  • 23
  • 32
  • I don’t know if it’s a bug, but I know that `GeometryReader` has added flexibility to otherwise inflexibly-sized subviews in all the betas. If you explain what you actually want to use `GeometryReader` for, maybe we can help you make it work better for you. For example, if you use the `GeometryReader` in an `overlay` or `background` modifier, the `GeometryReader` won’t be able to affect the layout of the modified view but will take on the modified view’s size. – rob mayoff Aug 20 '19 at 06:32
  • 1
    @robmayoff I currently have a hex view, which uses `GeometryReader` on overlay to calculate size of a single monospace character, and compose an even set of columns, depending on a view's geometry according to the size of a single character. There is no way to build that without `GeometryReader`. But what stumbles me in general, I, as a consumer of some view, might not even know that it measures geometry, put it in a `List` - and boom, it's broken. Doesn't sound right to me tbh. – rshev Aug 20 '19 at 22:39
  • See if [this answer](https://stackoverflow.com/a/56673501/77567) helps you. – rob mayoff Aug 20 '19 at 23:57
  • I just hit this when I used [this answer](https://stackoverflow.com/a/58474880/129889) to embed a multi-line UILabel in a List. – alltom Oct 20 '19 at 23:34
  • It seems odd to me that `GeometryReader` changes the behaviour of the nested item. Since `GeometryReader` actual reads the container that holds it I've had luck with making it an empty sibling inside a `Group`. `GeometryReader` seems especially weird inside of `ZStack` – Tony Topper May 07 '20 at 01:15

1 Answers1

2

Add min row height for the list.

List(1...5, id: \.self) { _ in
    GeometryReader { _ in
        self.multilineText
    }
}.environment(\.defaultMinListRowHeight, 100)
Nilay
  • 327
  • 3
  • 9