Tuesday, October 01, 2024

GeometryReader in SwiftUI

 GeometryReader is a SwiftUI tool that allows users to access and manipulate the size and position of a view.

Getting Screen Size:



struct ScreenSize: View {

    var body: some View {

        GeometryReader { geometry in

            Text("Screen width: \(geometry.size.width)")

                .padding()

        }

    }

}




Getting View Size:



struct ViewSize: View {

    var body: some View {

        GeometryReader { geometry in

            Rectangle()

                .fill(Color.blue)

                .overlay(

                    Text("View width: \(geometry.size.width)"),

                    alignment: .center

                )

        }

        .frame(width: 200, height: 100)

    }

}


Key Points:


1. GeometryReader provides size and position info.
2. Use geometry.size to access width and height.
3. Can be used within any view.
4. Global screen size access via UIScreen.main.bounds.

No comments:

Create a list in SwiftUI with sticky section headers

 Sample Code import SwiftUI struct ContentView : View {     @State var isCustomViewControllerDisplayed = false     @State private va...