Contents
Introduction                                                        13
    Don’t panic!
    What is SwiftUI?
    SwiftUI vs Interface Builder and storyboards
    Frequently asked questions about SwiftUI
    Answering the big question: should you learn SwiftUI, UIKit, or both?
    How to follow this quick start guide
    Migrating from UIKit to SwiftUI
    What’s in the basic template?
    Dedication
Building a complete project                                         33
    SwiftUI tutorial: Building a complete project
    Building a menu using List
    Composing views to create a list row
    Polishing designs with fonts and colors
    Displaying a detail screen with NavigationLink
    Observable objects, environment objects, and @Published
    Adding items to an order with @EnvironmentObject
    Adding TabView and tabItem()
    Bindings and forms
    Two-way bindings in SwiftUI
    Formatting interpolated strings in SwiftUI
    Presenting an alert
    Adding swipe to delete and EditButton
    Wrap up: our SwiftUI project is complete
Working with static text                                            95
    How to create static labels with a Text view
2                       www.hackingwithswift.com
  How to style text views with fonts, colors, line spacing, and more
  How to format text inside text views
  How to add spacing between letters in text
  How to format dates inside text views
  How to make TextField uppercase or lowercase using textCase()
  How to show text and an icon side by side using Label
  How to mark content as a placeholder using redacted()
Images, shapes, and other media                                   107
  How to draw images using Image views
  How to adjust the way an image is fitted to its space
  How to tile an image
  How to render images using SF Symbols
  How to render a gradient
  How to use images and other views as a backgrounds
  How to display solid shapes
  How to fill and stroke shapes at the same time
  How to draw part of a solid shape using trim()
  When should you use ContainerRelativeShape?
  How to play movies with VideoPlayer
  How to integrate SpriteKit using SpriteView
View layout                                                       128
  How to give a view a custom frame
  How to control spacing around individual views using padding
  How to provide relative sizes using GeometryReader
  How to place content outside the safe area
  How to return different view types
  How to create views in a loop using ForEach
  How to control layout priority using layoutPriority()
  How to make two views the same width or height
Stacks, grids, and scrollviews                                    145
  How to create stacks using VStack and HStack
                      www.hackingwithswift.com                          3
    How to customize stack layouts with alignment and spacing
    How to force views to one side inside a stack using Spacer
    How to make a fixed size Spacer
    How to layer views on top of each other using ZStack
    How to change the order of view layering using Z index
    How to create different layouts using size classes
    How to automatically switch between HStack and VStack based on
     size class
    How to add horizontal and vertical scrolling using ScrollView
    How to make a scroll view move to a location using ScrollViewReader
    How to create 3D effects like Cover Flow using ScrollView and
     GeometryReader
    How to position views in a grid using LazyVGrid and LazyHGrid
    How to lazy load views using LazyVStack and LazyHStack
Creating user interface controls                                  174
    Working with state
    How to create a tappable button
    How to disable the overlay color for images inside Button and
     NavigationLink
    How to read text from a TextField
    How to add a border to a TextField
    How to add a placeholder to a TextField
    How to disable autocorrect in a TextField
    How to dismiss the keyboard for a TextField
    How to format a TextField for numbers
    How to create secure text fields using SecureField
    How to create a toggle switch
    How to create a slider and read values from it
    How to create a picker and read values from it
    How to create a date picker and read values from it
    How to create a segmented control and read values from it
    How to create a stepper and read values from it
    How to hide the label of a Picker, Stepper, Toggle, and more using
     labelsHidden()
    How to create multi-line editable text with TextEditor
4                       www.hackingwithswift.com
  How to let users select a color with ColorPicker
  How to show progress on a task using ProgressView
  How to show indeterminate progress using ProgressView
  How to show a Map view
  How to show annotations in a Map view
  How to open web links in Safari
Responding to global and lifecycle events                      215
  How to detect when your app moves to the background or foreground
   with scenePhase
  How to respond to view lifecycle events: onAppear() and
   onDisappear()
  How to add keyboard shortcuts using keyboardShortcut()
  How to control which view is shown when your app launches
  How to run code when your app launches
  How to add an AppDelegate to a SwiftUI app
  How to detect device rotation
Taps and gestures                                              230
  How to add a gesture recognizer to a view
  How to read tap and double-tap gestures
  How to force one gesture to recognize before another using
   highPriorityGesture()
  How to make two gestures recognize at the same time using
   simultaneousGesture()
  How to create gesture chains using sequenced(before:)
  How to detect the user hovering over a view
  How to detect shake gestures
  How to control the tappable area of a view using contentShape()
  How to disable taps for a view using allowsHitTesting()
Advanced state                                                 247
  What’s the difference between @ObservedObject, @State, and
   @EnvironmentObject?
                     www.hackingwithswift.com                        5
    How to use @StateObject to create and monitor external objects
    How to use @ObservedObject to manage state from external objects
    How to use @EnvironmentObject to share data between views
    How to send state updates manually using objectWillChange
    How to create constant bindings
    How to create custom bindings
    How to use a timer with SwiftUI
    How to run some code when state changes using onChange()
Lists                                                         269
    Working with lists
    How to create a list of static items
    How to create a list of dynamic items
    How to let users delete rows from a list
    How to let users move rows in a list
    How to add sections to a list
    How to enable editing on a list using EditButton
    How to set the background color of list rows using
     listRowBackground()
    How to create grouped and inset grouped lists
    How to create expanding lists
    How to scroll to a specific row in a list
    How to allow row selection in a list
    How to use implicit stacking
Forms                                                         291
    Working with forms
    Basic form design
    Breaking forms into sections
    Pickers in forms
    Enabling and disabling elements in forms
    Showing and hiding form rows
Containers                                                    302
6                       www.hackingwithswift.com
Containers                                                        302
  Working with containers
  How to embed a view in a navigation view
  How to add bar items to a navigation view
  How to embed views in a tab bar using TabView
  How to create scrolling pages of content using tabViewStyle()
  How to group views together
  How to hide and show the status bar
  How to hide and reveal content using DisclosureGroup
  How to create a toolbar and add buttons to it
  How to add a sidebar for iPadOS
Alerts, action sheets, and menus                                  321
  Working with presentations
  How to show an alert
  How to add actions to alert buttons
  How to show multiple alerts in a single view
  How to show an action sheet
  How to show a context menu
  How to recommend another app using appStoreOverlay()
  How to show a menu when a button is pressed
  How to let users pick options from a menu
Presenting views                                                  336
  How to push a new view onto a NavigationView
  How to push a new view when a list row is tapped
  How to use programmatic navigation in SwiftUI
  How to present a new view using sheets
  How to make a view dismiss itself
  How to present a full screen modal view using fullScreenCover()
  How to show a popover view
Transforming views                                                351
  How to adjust the position of a view using its offset
  How to color the padding around a view
                       www.hackingwithswift.com                         7
    How to stack modifiers to create more advanced effects
    How to draw a border around a view
    How to draw a border inside a view
    How to create a marching ants border effect
    How to draw a shadow around a view
    How to clip a view so only part is visible
    How to rotate a view
    How to rotate a view in 3D
    How to scale a view up or down
    How to round the corners of a view
    How to adjust the opacity of a view
    How to adjust the accent color of a view
    How to mask one view with another
    How to blur a view
    How to blend views together
    How to adjust views by tinting, and desaturating, and more
    Customizing Button with ButtonStyle
    Customizing ProgressView with ProgressViewStyle
    Customizing Toggle with ToggleStyle
Drawing                                                          385
    SwiftUI’s built-in shapes
    How to draw a custom path
    How to draw polygons and stars
    How to draw a checkerboard
    How to use UIBezierPath and CGPath in SwiftUI
    How to convert a SwiftUI view to an image
Animation                                                        401
    How to create basic animations
    How to create a spring animation
    How to animate changes in binding values
    How to create an explicit animation
    How to delay an animation
    How to start an animation immediately after a view appears
8                      www.hackingwithswift.com
  How to apply multiple animations to a view
  How to synchronize animations from one view to another with
   matchedGeometryEffect()
  How to add and remove views with a transition
  How to combine transitions
  How to create asymmetric transitions
  How to create a custom transition
  How to animate the size of text
  How to override animations with transactions
Composing views                                                 436
  How to create and compose custom views
  How to combine text views together
  How to store views as properties
  How to create custom modifiers
  How to wrap a custom UIView for SwiftUI
  How to create modifiers for a UIViewRepresentable struct
  How to insert images into text
Cross-platform SwiftUI                                          451
  Learn once, apply anywhere
  How to get translucent lists on macOS
  How to make carousel lists on watchOS
  How to read the Digital Crown on watchOS using
   digitalCrownRotation()
Data                                                            457
  Introduction to using Core Data with SwiftUI
  How to configure Core Data to work with SwiftUI
  How to access a Core Data managed object context from a SwiftUI
    view
  How to create a Core Data fetch request using @FetchRequest
  How to filter Core Data fetch requests using a predicate
  How to add Core Data objects from SwiftUI views
                     www.hackingwithswift.com                         9
     How to delete Core Data objects from SwiftUI views
     How to limit the number of items in a fetch request
     How to create a document-based app using FileDocument and
      DocumentGroup
     How to export files using fileExporter()
     How to continue an NSUserActivity in SwiftUI
Accessibility                                                       485
     Introduction to accessibility with SwiftUI
     How to use Dynamic Type with a custom font
     How to detect the Reduce Motion accessibility setting
     How to detect dark mode
     How to use decorative images to reduce screen reader clutter
     How to reduce animations when requested
Tooling                                                             497
     How to preview your layout at different Dynamic Type sizes
     How to preview your layout in light and dark mode
     How to preview your layout in different devices
     How to preview your layout in a navigation view
     How to use Instruments to profile your SwiftUI code and identify slow
      layouts
What now?                                                           516
     SwiftUI tips and tricks
Appendix A                                                          532
     Understanding property wrappers in Swift and SwiftUI
     All SwiftUI property wrappers explained and compared
     What is the @State property wrapper?
     What is the @StateObject property wrapper?
     What is the @Published property wrapper?
     What is the @ObservedObject property wrapper?
10                        www.hackingwithswift.com
  What is the @EnvironmentObject property wrapper?
  What is the @Environment property wrapper?
  What is the @Binding property wrapper?
  What is the @GestureState property wrapper?
  What is the @FetchRequest property wrapper?
  What is the @AppStorage property wrapper?
  What is the @SceneStorage property wrapper?
  What is the @ScaledMetric property wrapper?
  What is the @UIApplicationDelegateAdaptor property wrapper?
Appendix B                                                        565
  Common SwiftUI errors and how to fix them
  How to fix “Cannot assign to property: 'self' is immutable”
  How to fix “Initializer 'init(_:rowContent:)' requires that ‘SomeType’
   conform to 'Identifiable’”
  How to fix “Ambiguous reference to member 'buildBlock()’”
  How to fix “Function declares an opaque return type, but has no return
   statements in its body from which to infer an underlying type”
  How to fix “Property declares an opaque return type, but has no
   initializer expression from which to infer an underlying type”
  How to fix “Modifying state during view update, this will cause
   undefined behavior”
  How to fix “Protocol 'View' can only be used as a generic constraint
   because it has Self or associated type requirements”
  How to fix “Fatal error: No ObservableObject of type SomeType found”
  How to fix “Cannot convert value of type 'String' to expected argument
   type 'Binding<String>’”
  How to fix “Cannot convert value of type 'String' to expected argument
   type 'Text'"
  How to fix “Referencing initializer 'init(wrappedValue:)' on
   'ObservedObject' requires that ‘SomeType’ conform to
   'ObservableObject'"
  How to fix “Cannot convert value of type '() -> ()' to expected argument
   type '() -> _’”
  How to fix “Missing argument for parameter 'content' in call”
  How to fix images not resizing
                      www.hackingwithswift.com                          11
     How to fix a Form Picker or a NavigationLink that isn’t tappable
12                       www.hackingwithswift.com