how to centre a view in swiftui in swift

To center a view in SwiftUI, you can use the .frame modifier with the alignment parameter set to .center. Here's an example:

main.swift
struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
    }
}
173 chars
7 lines

In this example, we're using the .frame modifier on the Text view and setting the alignment parameter to .center. This will center the text horizontally and vertically within its parent view.

You can adjust the maxWidth and maxHeight parameters to control the size of the view. If you set them to .infinity, the view will expand to fill its parent view.

gistlibby LogSnag