'Why should we use GeometryReader on background while using key preferences?And not geometry reader on the whole element?I

Why should we use GeometryReader on background while using key preferences?And not geometry reader on the whole element?In all tutorials we should add background or overlay, why?

var body: some View {
        VStack {
            Text("Hello, World!")
                .padding()
                .background(
                    GeometryReader { geometry in
                        Color.yellow
                            .preference(key: SizePreferenceKey.self, value: geometry.size)
                    }
                )
            
            Spacer().frame(height: 40)
            
            Text("w: \(Int(mySize.width)) h: \(Int(mySize.height))")
                .padding()
                .background(Color.green)
        }.onPreferenceChange(SizePreferenceKey.self) { newSize in
            print("The new child size is: \(newSize)")
            mySize = newSize
        }
    }


Solution 1:[1]

Because GeometryReader is a greedy View type, it would occupy all available space, there for we should control it with background or overlay to be limited to the size of view you are about to read the proxy of it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 swiftPunk