GeometryReader in SwiftUI

GeometryReader in SwiftUI is a container view that provides you with the size and position of its content relative to its parent view. This is particularly useful when you want to create responsive designs that adapt to various screen sizes and orientations.

Here's a basic example to demonstrate how to use GeometryReader. In this example, we will create a simple view where a text element dynamically sizes itself to be half the width of its parent view, thanks to the information provided by GeometryReader.

import SwiftUI

struct ContentView: View {
    var body: some View {
        // GeometryReader is used to read the geometry of the parent view
        GeometryReader { geometry in
            // Inside the GeometryReader, you can use the geometry proxy to access size, safeAreaInsets, and more.
            VStack {
                Text("Hello, World!")
                    // Set the width of the text to be half of the parent view's width.
                    .frame(width: geometry.size.width / 2)
                    // Center the text horizontally in the parent view.
                    .background(Color.green)
                Spacer()
            }
        }
        .background(Color.blue)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}