import SwiftUI
struct ContentView: View {
@State private var labelText = "Welcome to my App!"
var body: some View {
VStack(spacing: 20) {
Spacer()
Text(labelText)
.font(.title)
.multilineTextAlignment(.center)
Button("Tap Me") {
labelText = "You tapped the button!"
}
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(8)
Spacer()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.white)
.ignoresSafeArea()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}We use a @State variable labelText to hold the text shown on the screen. This allows the text to update when the button is tapped.
The VStack arranges the label and button vertically with some spacing. The Text view shows the current label text, and the Button changes the text when tapped.
We style the button with padding, background color, white text, and rounded corners for a friendly look.
The whole stack is centered vertically and horizontally using Spacer() views inside the VStack along with frame(maxWidth: .infinity, maxHeight: .infinity) to ensure the content is in the middle of the screen.
This simple app welcomes the user and responds to their tap by changing the message.