SwiftUI in A Nutshell A Quick Reference Guide For Beginners
SwiftUI in A Nutshell A Quick Reference Guide For Beginners
2 - SwiftUI
Version 1.0.0
SwiftUI
in a
Nutshell
A Quick Reference Guide
for Beginners
by Ishtiak Ahmed
A Quick Reference Guide for Beginners 01
// SwiftUI
// SwiftUI is a modern declarative user interface framework for building iOS,
iPadOS, macOS, watchOS, and tvOS apps using the Swift programming language.
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
SignInView()
}
}
}
// TEXT
Text("Welcome")
// DATE
Text(Date(), style: .date) // ADD SOME STYLE TO ASSET'S IMAGE
// "February 10, 2023" Image("avatar1")
.resizable()
// TIME .aspectRatio(contentMode: .fit)
Text(Date(), style: .time)
// "7:23 PM” .cornerRadius(2.0)
// LEBEL
// Add icon next to text // IMAGE FROM SF SYMBOLS
Label("SwiftUI in a Nutshell", Image(systemName: "water.waves")
systemImage: "water.waves")
// SHAPE
// RECTANGLE SHAPE
Rectangle()
.fill(.orange)
.frame(width: 100, height: 100) // ORGANIZE CHILD VIEWS VERTICALLY
// Put Views inside VStack
VStack {
// CIRCLE SHAPE Text("SwiftUI")
Circle() Text(“in a")
.fill(.orange) Text("Nutshell")
.frame(width: 100, height: 100) }
// TOGGLE
// User action for true and false
@State var isActive = true
Toggle(isOn: $isActive) {
Text("FaceID")
}.padding() // TEXT FIELD
// You need a state var to bind
@State var nameText = "Ron"
// BUTTON
Button( TextField($nameText)
action: {
print("Button Tapped!")
}, // ADD SOME STYLES TO THE TEXT FIELD
label: { Text("Add Review") } @State var nameText = ""
)
TextField($nameText)
.textFieldStyle(.roundedBorder)
// IMAGE BUTTON
Button(
action: { // SECURE TEXT FIELD/PASSWORD
print("Play Button Tapped!")
}, @State var password = ""
label: { Image(systemName: "play") } SecureField($password)
)
// TEXT EDITOR
// Supporting multi-line scrollable text input
@State var nameText = ""
TextEditor(text: $nameText)
// DATE PICKER
@State var currentDate = Date()
DatePicker(
$currentDate,
maximumDate: Date(),
displayedComponents: .date // STEPPER WITH DEFAULT INCREMENT
)
@State var progress = 0
// OPTION PICKER Stepper(value: $progress, in: 1...100) {
@State var animalIndex = 1 Text("Progress \(progress)")
Picker("Selected Animal", selection: $animalIndex) { }
ForEach(0 ..< animals.count) { index in
Text(animals[index])
.tag(index) // STEPPER WITH VARIABLE INCREMENT
} Stepper(
}.pickerStyle(.segmented) onIncrement: { progress += 3 },
// SLIDER onDecrement: { progress -= 5 },
@State var downloadProgress = 0 label: {
Slider( Text("Progress \(progress)")
value: $downloadProgress, }
in: 0...100,
onEditingChanged: { editing in )
print(editing)
} // STEPPER WITH VARIABLE INCREMENT
) @State var progress = 0
// PROGRESS Stepper(value: $progress,
ProgressView("Total downloads", value: 13, total: 50) in: 0...100.0,
step: 0.1) {
Text("Progress \(progress)")
}
// IMAGE BACKGROUND
Text("SwiftUI is Cool")
.font(.largeTitle)
.background(
Image(systemName: "play") // LAZY LOAD
.resizable() // LazyVStack
.frame(width: 100, height: 100) // Lazyly load contents in VStack
) ScrollView(.vertical) {
LazyVStack {
// GRADIENT BACKGROUND ForEach(0...1000) { number in
Text("SwiftUI is Cool") Text("Student \(number)")
.background(
LinearGradient( }
gradient: Gradient( }
colors: [.orange,.red,.yellow] }
),
startPoint: .leading, // LazyHStack
endPoint: .trailing // Lazyly load contents in HStack
), ScrollView(.horizontal) {
cornerRadius: 10 LazyHStack {
) ForEach(0...1000) { number in
Text("Student \(number)")
}
}
}
// GRID
// LazyVGrid LazyHGrid
var columns: [GridItem] = Array(
repeating: .init(.flexible(), alignment: .center),
count: 2
) // GESTURE
ScrollView { // TAP GESTURE
LazyVGrid(columns: columns, spacing: 8) { Text("Add")
ForEach(0...1000, id: \.self) { number in .gesture(
Text("Student \(number)") TapGesture()
} .onEnded { _ in
} print(“TAP GESTURE")
} }
)
// STATIC LIST
List {
// SCROLL VIEW
Text("Account") ScrollView(.horizontal) {
Text("My address")
Text("My orders") Text("White")
}
Text("Blue")
// DYNAMIC LIST
let colors = ["orange", "blue", "red"]
Text("Red")
List(colors, id: \.self) { color in }
Text(color)
}
SwiftUI UIKit
View UIView
Text UILabel
Label with Icon UILabel + ImageView
Link Not Available
Image UIImageView
TextField UITextField
Button UIButton
Toggle UISwitch
Stepper UIStepper
Slider UISlider
Picker UISegmentedControl
TextField UITextField
SecureTextField UITextField
TextEditor UITextView
DatePicker UIDatePicker
ProgressView UIProgressView
Alert UIAlertController
ActionSheet UIAlertController - actionSheet
VStack UIStackView - vertical
LazyVStack Not Available
HStack UIStackView - horizontal
LazyHStack Not Available
LazyVGrid UICollectionView - vertical
LazyHGrid UICollectionView - horizontal
NavigationView UINavigationController
List UITableView
TabView UITabBarController
Map MapKit
Charts Not Available
// SHORTCUTS
QUICK DOCUMENTATION
Option + Click (⌥ Click)
FILE INSPECTOR
Option + Command + 1 (⌥ ⌘ 1)
HISTORY INSPECTOR
Option + Command + 2 (⌥ ⌘ 2)
ATTRIBUTES INSPECTOR
Option + Command + 4 (⌥ ⌘ 4)
LEGAL
Written and published by Ishtiak Ahmed as eBook & digital, downloadable
format | Copyright © 2023 www.ishtiz.com / Ishtiak Ahmed
NEWSLETTER 👉 ishtiz.substack.com
If you have a fondness for free resources such as this guide, you're
welcome to sign up for my newsletter and receive a continuous flow of
curated tips on iOS, Swift, and SwiftUI, along with other free goodies.
Ishtiak Ahmed
Staff iOS Developer based in Munich, 🇩🇪
Website www.ishtiz.com
Twitter @ishtiz_
LinkedIn @ishtiakahmed
NEWSLETTER