50 Essential SwiftUI Commands with Examples and Explanations


🔹 Basic Views

  1. Text – display a string of text
Text("Hello World") // shows text on screen
  1. Image – show an image from assets or system
Image("logo") // displays image named "logo"
  1. Button – interactive button with action
Button("Click") { } // button that triggers code
  1. Spacer – pushes content apart
Spacer() // creates flexible empty space
  1. Divider – thin horizontal line
Divider() // separates content with a line
  1. Color – block of color
Color.red // fills area with red
  1. Rectangle – simple rectangle shape
Rectangle().fill(Color.blue) // blue box
  1. Circle – circular shape
Circle().stroke(Color.green, lineWidth: 3) // green outlined circle
  1. Ellipse – oval shape
Ellipse().fill(Color.orange) // orange oval
  1. Capsule – pill-like rounded shape
Capsule().fill(Color.purple) // purple pill shape

🔹 Layout Containers

  1. VStack – vertical layout stack
VStack {
    Text("A")
    Text("B")
    Text("C")
}
  1. HStack – horizontal layout stack
HStack {
    Text("Hello World")
    Text("Hello World")
    Text("Hello World")
}
  1. ZStack – layers content on top of each other
ZStack {
    Color.yellow
    Text("Top")
        .font(.title)
}
  1. Group – groups multiple views
Group {
    Text("One")
    Text("Two")
    Text("Three")
}
  1. ScrollView – scrollable content
ScrollView { Text("Scrollable") } // vertical scroll
  1. List – dynamic scrolling list
List(1..<4) { Text("Row \($0)") } // list rows
  1. Form – form layout for input controls
Form { TextField("Name", text: .constant("")) } // input form
  1. Section – divides list/form into sections
Section(header: Text("Info")) { Text("Details") } // sectioned content
  1. NavigationView – container for navigation
NavigationView { Text("Home") } // enables navigation
  1. NavigationLink – clickable link to another view
NavigationLink("Go", destination: Text("Next")) // link to new page

🔹 Input Controls

  1. TextField – single-line text input
TextField("Enter text", text: .constant("")) // text box
  1. SecureField – password input (hidden text)
SecureField("Password", text: .constant("")) // hidden input
  1. Toggle – on/off switch
Toggle("Enable", isOn: .constant(true)) // switch control
  1. Slider – sliding numeric value
Slider(value: .constant(0.5)) // slider 0 to 1
  1. Stepper – increase/decrease value
Stepper("Count", value: .constant(1)) // step counter
  1. Picker – dropdown or wheel selection
Picker("Choose", selection: .constant(1)) { Text("One").tag(1) } // picker
  1. DatePicker – pick date and/or time
DatePicker("Select Date", selection: .constant(Date())) // date selector
  1. ColorPicker – choose a color
ColorPicker("Pick Color", selection: .constant(.blue)) // color input
  1. ProgressView – loading/progress indicator
ProgressView("Loading…") // spinner/progress bar
  1. Link – open external URL
Link("Open Website", destination: URL(string: "https://kiaapp.xyz")!) // external link

🔹 Modifiers (Styling)

  1. .font() – set font style/size
Text("Title").font(.title) // larger font
  1. .foregroundColor() – set text or shape color
Text("Red").foregroundColor(.red) // red text
  1. .background() – add background behind view
Text("BG").background(Color.gray) // gray background
  1. .cornerRadius() – round corners
Rectangle().cornerRadius(10) // rounded box
  1. .padding() – add spacing inside view
Text("Padded").padding() // padded text
  1. .frame() – set width/height
Text("Boxed").frame(width:100, height:50) // fixed size
  1. .opacity() – adjust transparency
Text("Faded").opacity(0.5) // 50% transparent
  1. .shadow() – add shadow effect
Text("Shadow").shadow(radius: 5) // drop shadow
  1. .bold() / .italic() – emphasize text
Text("Bold").bold() // bold text
  1. .multilineTextAlignment() – align multi-line text
Text("Centered").multilineTextAlignment(.center) // center aligned

🔹 Advanced Containers & Features

  1. TabView – tab bar navigation
TabView {
   Text("Home").tabItem { Label("Home", systemImage: "house") }
} // tab navigation
  1. GeometryReader – access parent size/position
GeometryReader { geo in Text("\(geo.size.width)") } // get layout info
  1. LazyVStack – efficient vertical stack (on demand)
ScrollView { LazyVStack { Text("Item") } } // lazy vertical list
  1. LazyHStack – efficient horizontal stack
ScrollView(.horizontal) { LazyHStack { Text("Item") } } // lazy horizontal
  1. LazyVGrid – vertical grid layout
LazyVGrid(columns: [GridItem(.flexible())]) { Text("Cell") } // grid
  1. LazyHGrid – horizontal grid layout
LazyHGrid(rows: [GridItem(.flexible())]) { Text("Cell") } // horizontal grid
  1. Map (via MapKit) – show a map
Map(coordinateRegion: .constant(MKCoordinateRegion())) // map view
  1. VideoPlayer – play video content
VideoPlayer(player: AVPlayer(url: URL(string:"video.mp4")!)) // video player
  1. WebView (custom) – embed a web page
// Custom wrapper required for WKWebView
  1. Alert / Sheet – pop-up alerts or modal sheets
.alert("Title", isPresented: .constant(true)) { Button("OK", role:.cancel){} } // alert popup