Fucking SwiftUI - Cheat Sheet
Fucking SwiftUI - Cheat Sheet
Fucking SwiftUI
Fucking SwiftUI is a curated list of All the answers you found here don't mean to be complete or detail, the purpose
questions and answers about SwiftUI. here is to act as a cheat sheet or a place that you can pick up keywords you can
You can track change in Changelog use to search for more detail.
# FAQ
Frequently asked questions about SwiftUI.
iOS 14
A new WidgetKit framework in iOS 14 is exclusive to SwiftUI, so you might need to learn it this year if you want to support a new wi
iOS 14
iOS 14 closing some gap, but the point remains. You need to come back to UIKit when you hit a roadblock.
https://fuckingswiftui.com 1/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
# Minimum Requirements
Xcode 11 for SwiftUI and Xcode 12 beta for iOS 14 features (Download beta software from Apple)
iOS 13 / macOS 10.15 / tvOS 13 / watchOS 6
macOS Catalina in order to have SwiftUI render in the canvas.
Want to try beta SwiftUI features, but don't want to install a new beta OS on your machine
You can install beta OS in parallel to your current macOS version. Instruction here Installing macOS on a separate APFS volume
# View Controllers
UIKit SwiftUI Note
UIViewController View
UISplitViewController NavigationView
UINavigationController NavigationView
UITabBarController TabView
UISearchController -
UIImagePickerController -
UIVideoEditorController -
UIActivityViewController -
UIAlertController Alert
UITabBar TabView
https://fuckingswiftui.com 2/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
UIScrollView ScrollView
UIImageView Image
UIPickerView Picker
UIDatePicker DatePicker
UISlider Slider
UIStepper Stepper
UISwitch Toggle
UIView UIViewRepresentable
UIViewController UIViewControllerRepresentable
https://fuckingswiftui.com 3/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
# Pure SwiftUI
iOS 14
In iOS 14, you can write the whole app without a need for UIKit. Checkout App essentials in SwiftUI session from WWDC2020.
UIApplicationDelegate App
UIWindowSceneDelegate Scene
# Text
A view that displays one or more lines of read-only text.
Text("Hello World")
Styling
Text("Hello World")
.bold()
.italic()
.underline()
.lineLimit(2)
String provided in Text also used as LocalizedStringKey , so you get NSLocalizedString 's behavior for free.
To format text inside text view. Actually this is not SwiftUI feature, but Swift 5 String interpolation.
https://fuckingswiftui.com 4/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
Text alignment.
Text("Hello\nWorld!").multilineTextAlignment(.center)
Documentation - Text
# Label
iOS 14
Label is a convenient view that presents an image and text alongside each other. This is suitable for a menu item or your settings.
I can't find a way to adjust image size, and SF Symbols is misaligned with text in the first beta.
Documentation - Label
# TextField
A control that displays an editable text interface.
Documentation - TextField
# TextEditor
iOS 14
Documentation - TextEditor
# SecureField
https://fuckingswiftui.com 5/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
A control into which the user securely enters private text.
Documentation - SecureField
# Image
A view that displays an environment-dependent image.
Image(systemName: "clock.fill")
you can add style to system icon set to match font you use
Image(systemName: "cloud.heavyrain.fill")
.foregroundColor(.red)
.font(.title)
Image(systemName: "clock")
.foregroundColor(.red)
.font(Font.system(.largeTitle).bold())
Image("foo")
.resizable() // it will sized so that it fills all the available space
.aspectRatio(contentMode: .fit)
Documentation - Image
# Button
A control that performs an action when triggered.
Button(
action: {
// did tap
},
https://fuckingswiftui.com 6/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
label: { Text("Click Me") }
)
If your Button 's label is only Text you can initialize with this simpler signature.
Button("Click Me") {
// did tap
}
Button(action: {
}, label: {
Image(systemName: "clock")
Text("Click Me")
Text("Subtitle")
})
.foregroundColor(Color.white)
.padding()
.background(Color.blue)
.cornerRadius(5)
Documentation - Button
# Link
iOS 14
Create a link-style Button that will open in the associated app, if possible, but otherwise in the user’s default web browser.
Documentation - Link
# NavigationLink
A button that triggers a navigation presentation when pressed. This is a replacement for pushViewController
NavigationView {
NavigationLink(destination:
Text("Detail")
.navigationBarTitle(Text("Detail"))
) {
Text("Push")
}.navigationBarTitle(Text("Master"))
}
Or make it more readable by use group destination into it own view DetailView
https://fuckingswiftui.com 7/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
NavigationView {
NavigationLink(destination: DetailView()) {
Text("Push")
}.navigationBarTitle(Text("Master"))
}
Not sure if it is a bug or by design, in Beta 5 above code won't work. Try wrap NavigationLink in List like this to test the feature out.
NavigationView {
List {
NavigationLink(destination: Text("Detail")) {
Text("Push")
}.navigationBarTitle(Text("Master"))
}
}
If your NavigationLink 's label is only Text you can initialize with this simpler signature.
Documentation - NavigationLink
# ToolbarItem
iOS 14
A model that represents an item which can be placed in the toolbar or navigation bar. This represents most properties in UINaviga
Add titleView .
NavigationView {
Text("SwiftUI").padding()
.toolbar {
ToolbarItem(placement: .principal) {
VStack {
Text("Title")
Button("Clickable Subtitle") { print("principle") }
}
}
}
}
NavigationView {
Text("SwiftUI").padding()
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button {
} label: {
Image(systemName: "square.and.pencil")
https://fuckingswiftui.com 8/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
}
}
}
}
NavigationView {
Text("SwiftUI").padding()
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button {
} label: {
Image(systemName: "square.and.pencil")
}
}
ToolbarItem(placement: .navigationBarTrailing) {
Button {
} label: {
Image(systemName: "square.and.pencil")
}
}
}
}
Documentation - ToolbarItem
# Toggle
A control that toggles between on and off states.
Toggle(isOn: $isShowing) {
Text("Hello World")
}
If your Toggle 's label is only Text you can initialize with this simpler signature.
Documentation - Toggle
# Map
iOS 14
https://fuckingswiftui.com 9/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
Show map with a specified region
import MapKit
Map(coordinateRegion: $region)
You can control a map interaction by specify interactionModes (Use [] to disable all interactions).
Map(coordinateRegion: $region,
interactionModes: [],
showsUserLocation: true,
userTrackingMode: nil,
annotationItems: [PinItem(coordinate: .init(latitude: 37.334722, longitude: -122.008889))]) { item in
MapMarker(coordinate: item.coordinate)
}
Documentation - Map
# Picker
A control for selecting from a set of mutually exclusive values.
Picker style change based on its ancestor, under Form or List it appear as a single list row that you can tap to bring in a new sc
NavigationView {
Form {
Section {
Picker(selection: $selection, label:
Text("Picker Name")
, content: {
Text("Value 1").tag(0)
Text("Value 2").tag(1)
Text("Value 3").tag(2)
Text("Value 4").tag(3)
})
}
}
}
https://fuckingswiftui.com 10/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
ForEach(0 ..< settings.count) { index in
Text(self.settings[index])
.tag(index)
}
}.pickerStyle(SegmentedPickerStyle())
Documentation - Picker
# DatePicker
A control for selecting an absolute date.
Date Picker style also changes based on its ancestor. Under Form or List , it appears as a single list row that you can tap to exp
NavigationView {
Form {
Section {
DatePicker(
selection: $selectedDate,
in: dateClosedRange,
displayedComponents: .date,
label: { Text("Due Date") }
)
}
}
}
DatePicker(
selection: $selectedDate,
in: dateClosedRange,
displayedComponents: [.hourAndMinute, .date],
label: { Text("Due Date") }
)
https://fuckingswiftui.com 11/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
If your DatePicker 's label is only plain Text you can initialize with this simpler signature.
DatePicker("Due Date",
selection: $selectedDate,
in: dateClosedRange,
displayedComponents: [.hourAndMinute, .date])
minimumDate and maximumDate can be set using ClosedRange , PartialRangeThrough , and PartialRangeFrom .
DatePicker("Minimum Date",
selection: $selectedDate,
in: Date()...,
displayedComponents: [.date])
DatePicker("Maximum Date",
selection: $selectedDate,
in: ...Date(),
displayedComponents: [.date])
Documentation - DatePicker
# ProgressView
iOS 14
VStack {
ProgressView(value: progress)
Button("More", action: { progress += 0.05 })
}
ProgressView(value: progress)
.progressViewStyle(CircularProgressViewStyle())
Documentation - ProgressView
# Slider
A control for selecting a value from a bounded linear range of values.
https://fuckingswiftui.com 12/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
Slider lack of minimumValueImage and maximumValueImage , but we can replicate that easily by `HStack
Documentation - Slider
# Stepper
A control used to perform semantic increment and decrement actions.
If your Stepper 's label is only Text you can initialize with this simpler signature.
If you want full control, they offer bare bone Stepper where you manage your own data source.
If you also specify an amount of value for each step with initializers with step .
Documentation - Stepper
# HStack
A view that arranges its children in a horizontal line.
https://fuckingswiftui.com 13/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
Documentation - HStack
# LazyHStack
iOS 14
A view that arranges its children in a line that grows horizontally, creating items only as needed.
ScrollView(.horizontal) {
LazyHStack(alignment: .center, spacing: 20) {
ForEach(1...100, id: \.self) {
Text("Column \($0)")
}
}
}
Documentation - LazayHStack
# VStack
A view that arranges its children in a vertical line.
Documentation - VStack
# LazyVStack
iOS 14
A view that arranges its children in a line that grows vertically, creating items only as needed.
ScrollView {
LazyVStack(alignment: .leading) {
ForEach(1...100, id: \.self) {
Text("Row \($0)")
}
https://fuckingswiftui.com 14/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
}
}
Documentation - LazyVStack
# ZStack
A view that overlays its children, aligning them in both axes.
ZStack {
Text("Hello")
.padding(10)
.background(Color.red)
.opacity(0.8)
Text("World")
.padding(20)
.background(Color.red)
.offset(x: 0, y: 40)
}
}
Documentation - ZStack
# List
A container that presents rows of data arranged in a single column.
List {
Text("Hello world")
Text("Hello world")
Text("Hello world")
}
List {
Text("Hello world")
Image(systemName: "clock")
}
https://fuckingswiftui.com 15/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
To add section
List {
Section(header: Text("UIKit"), footer: Text("We will miss you")) {
Text("UITableView")
}
List {
Section(header: Text("UIKit"), footer: Text("We will miss you")) {
Text("UITableView")
}
To make it inset grouped ( .insetGrouped ), add .listStyle(GroupedListStyle()) and force regular horizontal size class .en
List {
Section(header: Text("UIKit"), footer: Text("We will miss you")) {
Text("UITableView")
}
iOS 14
.listStyle(InsetGroupedListStyle())
Documentation - List
# ScrollView
A view that allows the scrolling of its contained views.
https://fuckingswiftui.com 16/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
ScrollView {
Image("foo")
Text("Hello World")
}
# LazyHGrid
iOS 14
A container view that arranges its child views in a grid that grows horizontally, creating items only as needed.
ScrollView(.horizontal) {
LazyHGrid(rows: rows, alignment: .top) {
ForEach((0...100), id: \.self) {
Text("\($0)").background(Color.pink)
}
}
}
Documentation - LazyHGrid
# LazyVGrid
iOS 14
A container view that arranges its child views in a grid that grows vertically, creating items only as needed.
ScrollView {
LazyVGrid(columns: columns) {
ForEach((0...100), id: \.self) {
Text("\($0)").background(Color.pink)
}
}
}
Documentation - LazyVGrid
# Form
A container for grouping controls used for data entry, such as in settings or inspectors.
You can put almost anything into this Form and it will render appropriate style for a form.
https://fuckingswiftui.com 17/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
NavigationView {
Form {
Section {
Text("Plain Text")
Stepper(value: $quantity, in: 0...10, label: { Text("Quantity") })
}
Section {
DatePicker($date, label: { Text("Due Date") })
Picker(selection: $selection, label:
Text("Picker Name")
, content: {
Text("Value 1").tag(0)
Text("Value 2").tag(1)
Text("Value 3").tag(2)
Text("Value 4").tag(3)
})
}
}
}
Documentation - Form
# Spacer
A flexible space that expands along the major axis of its containing stack layout, or on both axes if not contained in a stack.
HStack {
Image(systemName: "clock")
Spacer()
Text("Time")
}
Documentation - Spacer
# Divider
A visual element that can be used to separate other content.
HStack {
Image(systemName: "clock")
Divider()
Text("Time")
}.fixedSize()
Documentation - Divider
# NavigationView
A view for presenting a stack of views representing a visible path in a navigation hierarchy.
NavigationView {
List {
Text("Hello World")
}
https://fuckingswiftui.com 18/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
.navigationBarTitle(Text("Navigation Title")) // Default to large title style
}
NavigationView {
List {
Text("Hello World")
}
.navigationBarTitle(Text("Navigation Title"), displayMode: .inline)
}
Add UIBarButtonItem
NavigationView {
List {
Text("Hello World")
}
.navigationBarItems(trailing:
Button(action: {
// Add action
}, label: {
Text("Add")
})
)
.navigationBarTitle(Text("Navigation Title"))
}
Use as UISplitViewController .
NavigationView {
List {
NavigationLink("Go to detail", destination: Text("New Detail"))
}.navigationBarTitle("Master")
Text("Placeholder for Detail")
}
You can style a NavigationView using two new style properties: stack and doubleColumn . By default, navigation views on iPhon
while on iPad and Mac, a split-view styled navigation view displays.
NavigationView {
MyMasterView()
MyDetailView()
}
.navigationViewStyle(StackNavigationViewStyle())
iOS 14
https://fuckingswiftui.com 19/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
In iOS 14, there is a new sidebar style for UISplitViewController . You can also do that by putting three views under Navigati
NavigationView {
Text("Sidebar")
Text("Primary")
Text("Detail")
}
Documentation - NavigationView
iOS 14
NavigationView {
Text("SwiftUI").padding()
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button {
} label: {
Image(systemName: "archivebox")
}
}
ToolbarItem(placement: .bottomBar) {
Spacer()
}
ToolbarItem(placement: .bottomBar) {
Button {
} label: {
Image(systemName: "square.and.pencil")
}
}
}
}
Documentation - ToolbarItem
# TabView
A view that allows for switching between multiple child views using interactable user interface elements.
TabView {
Text("First View")
.font(.title)
.tabItem({ Text("First") })
.tag(0)
Text("Second View")
.font(.title)
.tabItem({ Text("Second") })
.tag(1)
}
https://fuckingswiftui.com 20/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
Image and Text together. You can use SF Symbol here.
TabView {
Text("First View")
.font(.title)
.tabItem({
Image(systemName: "circle")
Text("First")
})
.tag(0)
Text("Second View")
.font(.title)
.tabItem(VStack {
Image("second")
Text("Second")
})
.tag(1)
}
TabView {
Text("First View")
.font(.title)
.tabItem({
Image(systemName: "circle")
Text("First")
})
.tag(0)
Text("Second View")
.font(.title)
.tabItem({
Image("second")
Text("Second")
})
.tag(1)
}
# UIPageViewController
UIPageViewController become a style of TabView . To use page view style, use .tabViewStyle(PageTabViewStyle()) mod
TabView {
Text("1")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.pink)
Text("2")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.red)
Text("3")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green)
Text("4")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.blue)
}.tabViewStyle(PageTabViewStyle())
https://fuckingswiftui.com 21/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
This PageTabViewStyle will include UIPageControl at the bottom just like UIPageViewController . To remove it, pass index
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
There is a new style for page control which will render background around the indicator. To enforce this new style, add
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always)) modifier to your tab view.
TabView {
Text("1")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.pink)
Text("2")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.red)
Text("3")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green)
Text("4")
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.blue)
}
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
.tabViewStyle(PageTabViewStyle())
Documentation - TabView
# Alert
A container for an alert presentation.
Button("Alert") {
self.isError = true
}.alert(isPresented: $isError, content: {
Alert(title: Text("Error"), message: Text("Error Reason"), dismissButton: .default(Text("OK")))
})
https://fuckingswiftui.com 22/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
dismissButton: .default(Text("OK"))
)
}
Documentation - Alert
# Modal
A modal transition.
Button("Modal") {
self.isModal = true
}.sheet(isPresented: $isModal, content: {
self.modal
})
Documentation - Sheet
https://fuckingswiftui.com 23/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
Documentation - Sheet
iOS 14
If you want the old style of modal presentation where a modal view is presented full screen, you can use .fullScreenCover inste
Since the full-screen cover style doesn't allow a user to use gesture to dismiss the modal, you have to add a way to dismiss the pre
add a button to dismiss the presented view by set isModal to false.
Button("Fullscreen") {
self.isModal = true
}.fullScreenCover(isPresented: $isFullscreen, content: {
self.modal
})
If you use a custom view as a modal, you can dismiss the presented view using the presentationMode environmental key.
Documentation - fullScreenCover
# ActionSheet
A storage type for an action sheet presentation.
https://fuckingswiftui.com 24/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
}),
.destructive(Text("Delete"), action: {
})
]
)
}
Button("Action Sheet") {
self.isSheet = true
}.actionSheet(isPresented: $isSheet, content: {
self.actionSheet
})
}),
.destructive(Text("Delete"), action: {
})
]
)
}
Documentation - ActionSheet
https://fuckingswiftui.com 25/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
# UIViewRepresentable
A view that represents a UIKit view. Use this when you want to use UIView inside SwiftUI.
To make any UIView usable in SwiftUI, create a wrapper view that conforms UIViewRepresentable .
import UIKit
import SwiftUI
return v
}
If you want to bridge between UIKit data binding (delegate, target/action) and SwiftUI, use Coordinator . Detail can be found in S
import SwiftUI
import UIKit
return control
}
https://fuckingswiftui.com 26/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
class Coordinator: NSObject {
var control: PageControl
Documentation - UIViewRepresentable
# UIViewControllerRepresentable
A view that represents a UIKit view controller. Use this when you want to use UIViewController inside SwiftUI.
To make any UIViewController usable in SwiftUI, create a wrapper view that conforms UIViewControllerRepresentable . Detail c
import SwiftUI
import UIKit
return pageViewController
}
Documentation - UIViewControllerRepresentable
# UIHostingController
A UIViewController that represents a SwiftUI view.
https://fuckingswiftui.com 27/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
Documentation - UIHostingController
# App
iOS 14
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
Text("Hello, world!")
}
}
}
You can also create custom scenes. To include more than one scene in an app, add the @SceneBuilder attribute to the body.
@main
struct Mail: App {
@SceneBuilder var body: some Scene {
WindowGroup {
MailViewer()
}
Settings {
SettingsView()
}
}
}
Documentation - App
# Scene
iOS 14
A part of an app’s user interface with a life cycle managed by the system.
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
Text("Hello, world!")
}
https://fuckingswiftui.com 28/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
}
}
Documentation - Scene
# Resources
API Documentation
Official Tutorials
WWDC 2019
Introducing SwiftUI: Building Your First App
SwiftUI Essentials
Data Flow Through SwiftUI
Building Custom Views with SwiftUI
Integrating SwiftUI
Accessibility in SwiftUI
SwiftUI On All Devices
SwiftUI on watchOS
Mastering Xcode Previews
WWDC 2020
Introduction to SwiftUI
What's new in SwiftUI
Build complications in SwiftUI
App essentials in SwiftUI
Visually edit SwiftUI views
Stacks, Grids, and Outlines in SwiftUI
Build a SwiftUI view in Swift Playgrounds
Build document-based apps in SwiftUI
Data Essentials in SwiftUI
Build SwiftUI views for widgets
WWDC 2021
https://fuckingswiftui.com 29/30
2/27/25, 5:25 PM Fucking SwiftUI - Cheat Sheet
Fucking SwiftUI by Sarun Wongpatcharapakorn. If you have any suggestions or questions feel free to contact me @sarunw /
Email.
https://fuckingswiftui.com 30/30