0

I'm working on a LiveActivity for an iOS 16 app using SwiftUI. I can't figure out what's going on with the GeometryReader. I'm trying to get the dimensions of the entire LiveActivity view.

In Example (A) below, I've got a VStack which is set to take up max width and height and contains a GeometryReader. I'm expecting to see geo.size.height be 160 (that's the max height of a LiveActivity on an iPhone 14 Pro Max).

Weirdly, as soon as I include the GeoReader either here or virtually anywhere else in the view tree, the LiveActivity view height collapses to 64 regardless of any changes in any other views.

//EXAMPLE (A)
struct LiveActivity: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(for: Attributes.self) { context in
            VStack{
                GeometryReader { geo in
                    Text("Geo Height \(geo.size.height)") //REPORTS 64
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
        } dynamicIsland: { context in
            ...
        }
    }
}

//EXAMPLE (B)
struct LiveActivity: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(for: Attributes.self) { context in
            VStack{
                Text("Example A")
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .overlay{
                GeometryReader { geo in
                    Text("Geo Height \(geo.size.height)") //REPORTS 160
                }
            }
        } dynamicIsland: { context in
            ...
        }
    }
}

I've tried all manner of ways to get everything to expand to the full size but having the GeoReader anywhere involved somehow negates everything else.

Is this a bug? Is this expected behaviour and I'm missing something? I would expect Example A to report 160 and be the full size.

Kaushik Makwana
  • 142
  • 1
  • 14
Hunter
  • 1
  • 2
  • 1
    GeometryReader is discouraged for iOS 16+ look at SwiftUI.Layout. – lorem ipsum Apr 20 '23 at 22:28
  • @loremipsum that is simply just not true. GeometryReader and the Layout protocol solve two totally different problems. – magnuskahr Apr 21 '23 at 11:56
  • @magnuskahr it was in one of the layout videos that Apple had last WWDC. I don’t remember which one. The OP hasn’t specified exactly what they are using it for but if it is used to layout views Layout is the tool. The only mention is to use Max size which implies it is being used to Layout a View. – lorem ipsum Apr 21 '23 at 12:05
  • 1
    The quote you are looking for is: “(…) the Layout protocol solves a problem that you might have tried to use geometry reader for in the past. (…) However, it's not the best choice in this case. And that's because a geometry reader is designed to measure its container view, and report the that size to its subview. The subview then uses the information to draw its own content. Notice that for the intended use of a geometry reader, the information flows downward.” As said - they solve different problems. – magnuskahr Apr 22 '23 at 13:12
  • @magnuskahr did you find a solution? – erotsppa Jul 08 '23 at 04:41
  • @erotsppa I haven't looked into it no, I only commented to correct lorumipsum. However, do you experience the same issue as OP? – magnuskahr Jul 10 '23 at 07:18
  • @magnuskahr Yes geometeyreader doesn’t work in widgets – erotsppa Jul 10 '23 at 18:42

0 Answers0