SwiftUI Cookbook

Book description

Unleash the power of declarative programming in SwiftUI with practical recipes for building cross-platform Apple applications for iOS 14, macOS, and watchOS using Swift 5.3, Xcode 12, and SwiftUI 2.0

Key Features

  • Apply the declarative programming paradigm for building cross-platform UIs for Apple devices
  • Learn to integrate UIKit, Core Data, Sign in with Apple, and Firebase with SwiftUI
  • Adopt the new SwiftUI 2.0 features to build visually appealing UIs at speed

Book Description

SwiftUI is an innovative and simple way to build beautiful user interfaces (UIs) for all Apple platforms, right from iOS and macOS through to watchOS and tvOS, using the Swift programming language. In this recipe-based book, you'll work with SwiftUI and explore a range of essential techniques and concepts that will help you through the development process. The recipes cover the foundations of SwiftUI as well as the new SwiftUI 2.0 features introduced in iOS 14. Other recipes will help you to make some of the new SwiftUI 2.0 components backward-compatible with iOS 13, such as the Map View or the Sign in with Apple View.

The cookbook begins by explaining how to use basic SwiftUI components. Then, you'll learn the core concepts of UI development such as Views, Controls, Lists, and ScrollViews using practical implementation in Swift. By learning drawings, built-in shapes, and adding animations and transitions, you'll discover how to add useful features to the SwiftUI. When you're ready, you'll understand how to integrate SwiftUI with exciting new components in the Apple development ecosystem, such as Combine for managing events and Core Data for managing app data. Finally, you'll write iOS, macOS, and watchOS apps while sharing the same SwiftUI codebase.

By the end of this SwiftUI book, you'll have discovered a range of simple, direct solutions to common problems found in building SwiftUI apps.

What you will learn

  • Explore various layout presentations in SwiftUI such as HStack, VStack, LazyHStack, and LazyVGrid
  • Create a cross-platform app for iOS, macOS, and watchOS
  • Get up to speed with drawings in SwiftUI using built-in shapes, custom paths, and polygons
  • Discover modern animation and transition techniques in SwiftUI
  • Add user authentication using Firebase and Sign in with Apple
  • Handle data requests in your app using Core Data
  • Solve the most common SwiftUI problems, such as integrating a MapKit map, unit testing, snapshot testing, and previewing layouts

Who this book is for

This book is for mobile developers who want to learn SwiftUI as well as experienced iOS developers transitioning from UIKit to SwiftUI. The book assumes knowledge of the Swift programming language. Knowledge of object-oriented design and data structures is useful but not necessary.

Table of contents

  1. SwiftUI Cookbook
  2. Why subscribe?
  3. Contributors
  4. About the authors
  5. About the reviewer
  6. Packt is searching for authors like you
  7. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Conventions used
    6. Get in touch
    7. Reviews
  8. Chapter 1: Using the Basic SwiftUI Views and Controls
    1. Technical requirements
    2. How to lay out components
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Dealing with text
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    4. Using images
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    5. Adding buttons and navigating with them
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    6. Beyond buttons – how to use advanced pickers
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. How to apply groups of styles using ViewModifiers
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    8. Separating presentation from content with ViewBuilder
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    9. Simple graphics using SF Symbols
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    10. The best of both worlds – integrating UIKit into SwiftUI
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    11. Adding SwiftUI to an existing app
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. More views and controls (iOS 14+)
      1. Getting ready
      2. How to do it…
      3. How it works…
  9. Chapter 2: Going Beyond the Single Component with Lists and Scroll Views
    1. Technical requirements
    2. Using scroll views
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Creating a list of static items
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Using custom rows in a list
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Adding rows to a list
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Deleting rows from a list
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    7. Editing a list
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    8. Moving rows in a list
      1. Getting ready
      2. How to do it…
      3. How it works…
    9. Adding sections to a list
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. Using LazyHStack and LazyVStack (iOS 14+)
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    11. Using LazyHGrid and LazyVGrid (iOS 14+)
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. Using ScrollViewReader (iOS 14+)
      1. Getting ready
      2. How to do it…
      3. How it works…
    13. Using expanding lists (iOS 14+)
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    14. Using disclosure groups to hide and show content (iOS 14+)
      1. Getting ready
      2. How to do it…
      3. How it works…
  10. Chapter 3: Viewing while Building with SwiftUI Preview
    1. Technical requirements
    2. Previewing the layout in dark mode
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Previewing the layout at different dynamic type sizes
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    4. Previewing the layout in a navigation view
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Previewing the layout on different devices
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Using previews in UIKit
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Using mock data for previews
      1. Getting ready
      2. How to do it…
      3. How it works…
  11. Chapter 4: Creating New Components and Grouping Views in Container Views
    1. Technical requirements
    2. Showing and hiding sections in forms
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more
    3. Disabling/enabling items in a form
      1. Getting ready
      2. How to do it…
      3. How it works
      4. There's more
    4. Navigating between multiple views with TabView
      1. Getting ready
      2. How to do it
      3. How it works
      4. There's more
    5. Using gestures with TabView
      1. Getting ready
      2. How to do it
      3. How it works
  12. Chapter 5: Presenting Extra Information to the User
    1. Technical requirements
    2. Presenting alerts
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
    3. Adding actions to alert buttons
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
    4. Presenting multiple alerts
      1. Getting ready
      2. How to do it
      3. How it works
    5. Presenting ActionSheet views
      1. Getting ready
      2. How to do it
      3. How it works
    6. Showing a sheet modally
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
    7. Creating a context menu
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
    8. Implementing a popover
      1. Getting ready
      2. How to do it
      3. How it works
      4. See also
  13. Chapter 6: Drawing with SwiftUI
    1. Technical requirements
    2. Using SwiftUI's built-in shapes
      1. Getting ready
      2. How to do it...
      3. How it works
    3. Creating a dashed border in SwiftUI
      1. Getting ready
      2. How to do it...
      3. How it works
    4. Drawing a custom shape
      1. Getting ready
      2. How to do it...
      3. How it works
    5. Drawing a curved custom shape
      1. Getting ready
      2. How to do it...
      3. How it works
    6. Using UIBezierPath with SwiftUI
      1. Getting ready
      2. How to do it...
      3. How it works
      4. There's more
    7. Implementing a progress ring
      1. Getting ready
      2. How to do it...
      3. How it works
    8. Implementing a Tic-Tac-Toe game in SwiftUI
      1. Getting ready
      2. How to do it...
      3. How it works
      4. There's more
    9. Rendering a gradient view in SwiftUI
      1. Getting ready
      2. How to do it...
      3. How it works
      4. There's more
    10. Rendering a border with a gradient
      1. Getting ready
      2. How to do it...
      3. How it works
    11. Filling a border with an image
      1. Getting ready
      2. How to do it...
      3. How it works
    12. Building a bar chart
      1. Getting ready
      2. How to do it...
      3. How it works
      4. There's more
    13. Building a pie chart
      1. Getting ready
      2. How to do it...
      3. How it works
  14. Chapter 7: Animating with SwiftUI
    1. Technical requirements
    2. Creating basic animations
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
      5. See also
    3. Transforming shapes
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Creating a banner with a spring animation
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Applying a delay to a view modifier animation to create a sequence of animations
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Applying a delay to a withAnimation function to create a sequence of animations
      1. Getting ready
      2. How to do it…
      3. How it works…
    7. Applying multiple animations to a view
      1. Getting ready
      2. How to do it…
      3. How it works…
    8. Creating custom view transitions
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    9. Creating a hero view transition with .matchedGeometryEffect
      1. Getting ready
      2. How to do it…
      3. How it works…
    10. Creating an animated pressable button
      1. Getting ready
      2. How to do it…
      3. How it works…
    11. Lottie animations in SwiftUI
      1. Getting ready
      2. How to do it…
      3. How it works…
    12. Implementing a stretchable header in SwiftUI
      1. Getting ready
      2. How to do it…
      3. How it works…
    13. Creating floating hearts in SwiftUI
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    14. How to implement a swipeable stack of cards in SwiftUI
      1. Getting ready
      2. How to do it…
      3. How it works…
  15. Chapter 8: Driving SwiftUI with Data
    1. Technical requirements
    2. Using @State to drive Views behavior
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Using @Binding to pass a state variable to child Views
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Implementing a CoreLocation wrapper as @ObservedObject
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Sharing state objects with multiple views using @EnvironmentObject
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
  16. Chapter 9: Driving SwiftUI with Combine
    1. Technical requirements
    2. Introducing Combine in a SwiftUI project
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Managing the memory in Combine to build a timer app
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Validating a form using Combine
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Fetching remote data using Combine and visualizing it in SwiftUI
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    6. Debugging an app based on Combine
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    7. Unit testing an app based on Combine
      1. Getting ready
      2. How to do it…
      3. How it works…
  17. Chapter 10: Handling Authentication and Firebase with SwiftUI
    1. Technical requirements
    2. Implementing SwiftUI Sign in with Apple
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Implementing UIKit Sign in with Apple to be used in SwiftUI
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Integrating Firebase into a SwiftUI project
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
    5. Using Firebase to sign in using Google
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Implementing a distributed Notes app with Firebase and SwiftUI
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. There's more…
  18. Chapter 11: Handling Core Data in SwiftUI
    1. Technical requirements
    2. Integrating Core Data with SwiftUI
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Showing Core Data objects with @FetchRequest
      1. Getting ready
      2. How to do it…
      3. How it works…
    4. Adding Core Data objects to a SwiftUI view
      1. Getting ready
      2. How to do it…
      3. How it works…
    5. Filtering Core Data requests using a predicate
      1. Getting ready
      2. How to do it…
      3. How it works…
    6. Deleting Core Data objects from a SwiftUI view
      1. Getting ready
      2. How to do it…
      3. How it works…
  19. Chapter 12: Cross-Platform SwiftUI
    1. Technical requirements
    2. Creating an iOS app in SwiftUI
      1. Getting ready
      2. How to do it
      3. How it works
    3. Creating the macOS version of the iOS app
      1. Getting ready
      2. How to do it
      3. How it works
    4. Creating the watchOS version of the iOS app
      1. Getting ready
      2. How to do it
      3. How it works
  20. Chapter 13: SwiftUI Tips and Tricks
    1. Technical requirements
    2. Snapshot testing SwiftUI views
      1. Getting ready
      2. How to do it…
      3. How it works…
    3. Unit testing SwiftUI with ViewInspector
      1. Getting ready
      2. How to do it
      3. How it works
    4. Implementing a multilanguage app with localized strings in SwiftUI
      1. Getting ready
      2. How to do it
      3. How it works
    5. Showing a PDF in SwiftUI
      1. Getting ready
      2. How to do it
      3. How it works
      4. There's more
    6. Embedding a MapView in SwiftUI
      1. Getting ready
      2. How to do it
      3. How it works
    7. Embedding a UIKit MapView in SwiftUI
      1. Getting ready
      2. How to do it
      3. How it works
    8. Implementing SwiftUI views using Playground
      1. Getting ready
      2. How to do it
      3. How it works
    9. Using custom fonts in SwiftUI
      1. Getting ready
      2. How to do it
      3. How it works
    10. Implementing asynchronous images in SwiftUI
      1. Getting ready
      2. How to do it
      3. How it works
      4. There's more
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: SwiftUI Cookbook
  • Author(s): Giordano Scalzo, Edgar Nzokwe
  • Release date: October 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781838981860