0%

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
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. Dealing with text
    7. Getting ready
    8. How to do it…
    9. How it works…
    10. There's more…
    11. See also
    12. Using images
    13. Getting ready
    14. How to do it…
    15. How it works…
    16. See also
    17. Adding buttons and navigating with them
    18. Getting ready
    19. How to do it…
    20. How it works…
    21. See also
    22. Beyond buttons – how to use advanced pickers
    23. Getting ready
    24. How to do it…
    25. How it works…
    26. How to apply groups of styles using ViewModifiers
    27. Getting ready
    28. How to do it…
    29. How it works…
    30. See also
    31. Separating presentation from content with ViewBuilder
    32. Getting ready
    33. How to do it…
    34. How it works…
    35. See also
    36. Simple graphics using SF Symbols
    37. Getting ready
    38. How to do it…
    39. How it works…
    40. See also
    41. The best of both worlds – integrating UIKit into SwiftUI
    42. Getting ready
    43. How to do it…
    44. How it works…
    45. See also
    46. Adding SwiftUI to an existing app
    47. Getting ready
    48. How to do it…
    49. How it works…
    50. More views and controls (iOS 14+)
    51. Getting ready
    52. How to do it…
    53. How it works…
  9. Chapter 2: Going Beyond the Single Component with Lists and Scroll Views
    1. Technical requirements
    2. Using scroll views
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. See also
    7. Creating a list of static items
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. Using custom rows in a list
    12. Getting ready
    13. How to do it…
    14. How it works…
    15. Adding rows to a list
    16. Getting ready
    17. How to do it…
    18. How it works…
    19. Deleting rows from a list
    20. Getting ready
    21. How to do it…
    22. How it works…
    23. There's more…
    24. Editing a list
    25. Getting ready
    26. How to do it…
    27. How it works…
    28. There's more…
    29. Moving rows in a list
    30. Getting ready
    31. How to do it…
    32. How it works…
    33. Adding sections to a list
    34. Getting ready
    35. How to do it…
    36. How it works…
    37. Using LazyHStack and LazyVStack (iOS 14+)
    38. Getting ready
    39. How to do it…
    40. How it works…
    41. There's more…
    42. Using LazyHGrid and LazyVGrid (iOS 14+)
    43. Getting ready
    44. How to do it…
    45. How it works…
    46. Using ScrollViewReader (iOS 14+)
    47. Getting ready
    48. How to do it…
    49. How it works…
    50. Using expanding lists (iOS 14+)
    51. Getting ready
    52. How to do it…
    53. How it works…
    54. There's more…
    55. See also
    56. Using disclosure groups to hide and show content (iOS 14+)
    57. Getting ready
    58. How to do it…
    59. How it works…
  10. Chapter 3: Viewing while Building with SwiftUI Preview
    1. Technical requirements
    2. Previewing the layout in dark mode
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. Previewing the layout at different dynamic type sizes
    7. Getting ready
    8. How to do it…
    9. How it works…
    10. See also
    11. Previewing the layout in a navigation view
    12. Getting ready
    13. How to do it…
    14. How it works…
    15. Previewing the layout on different devices
    16. Getting ready
    17. How to do it…
    18. How it works…
    19. Using previews in UIKit
    20. Getting ready
    21. How to do it…
    22. How it works…
    23. Using mock data for previews
    24. Getting ready
    25. How to do it…
    26. 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
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. There's more
    7. Disabling/enabling items in a form
    8. Getting ready
    9. How to do it…
    10. How it works
    11. There's more
    12. Navigating between multiple views with TabView
    13. Getting ready
    14. How to do it
    15. How it works
    16. There's more
    17. Using gestures with TabView
    18. Getting ready
    19. How to do it
    20. How it works
  12. Chapter 5: Presenting Extra Information to the User
    1. Technical requirements
    2. Presenting alerts
    3. Getting ready
    4. How to do it
    5. How it works
    6. See also
    7. Adding actions to alert buttons
    8. Getting ready
    9. How to do it
    10. How it works
    11. See also
    12. Presenting multiple alerts
    13. Getting ready
    14. How to do it
    15. How it works
    16. Presenting ActionSheet views
    17. Getting ready
    18. How to do it
    19. How it works
    20. Showing a sheet modally
    21. Getting ready
    22. How to do it
    23. How it works
    24. See also
    25. Creating a context menu
    26. Getting ready
    27. How to do it
    28. How it works
    29. See also
    30. Implementing a popover
    31. Getting ready
    32. How to do it
    33. How it works
    34. See also
  13. Chapter 6: Drawing with SwiftUI
    1. Technical requirements
    2. Using SwiftUI's built-in shapes
    3. Getting ready
    4. How to do it...
    5. How it works
    6. Creating a dashed border in SwiftUI
    7. Getting ready
    8. How to do it...
    9. How it works
    10. Drawing a custom shape
    11. Getting ready
    12. How to do it...
    13. How it works
    14. Drawing a curved custom shape
    15. Getting ready
    16. How to do it...
    17. How it works
    18. Using UIBezierPath with SwiftUI
    19. Getting ready
    20. How to do it...
    21. How it works
    22. There's more
    23. Implementing a progress ring
    24. Getting ready
    25. How to do it...
    26. How it works
    27. Implementing a Tic-Tac-Toe game in SwiftUI
    28. Getting ready
    29. How to do it...
    30. How it works
    31. There's more
    32. Rendering a gradient view in SwiftUI
    33. Getting ready
    34. How to do it...
    35. How it works
    36. There's more
    37. Rendering a border with a gradient
    38. Getting ready
    39. How to do it...
    40. How it works
    41. Filling a border with an image
    42. Getting ready
    43. How to do it...
    44. How it works
    45. Building a bar chart
    46. Getting ready
    47. How to do it...
    48. How it works
    49. There's more
    50. Building a pie chart
    51. Getting ready
    52. How to do it...
    53. How it works
  14. Chapter 7: Animating with SwiftUI
    1. Technical requirements
    2. Creating basic animations
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. There's more…
    7. See also
    8. Transforming shapes
    9. Getting ready
    10. How to do it…
    11. How it works…
    12. Creating a banner with a spring animation
    13. Getting ready
    14. How to do it…
    15. How it works…
    16. Applying a delay to a view modifier animation to create a sequence of animations
    17. Getting ready
    18. How to do it…
    19. How it works…
    20. Applying a delay to a withAnimation function to create a sequence of animations
    21. Getting ready
    22. How to do it…
    23. How it works…
    24. Applying multiple animations to a view
    25. Getting ready
    26. How to do it…
    27. How it works…
    28. Creating custom view transitions
    29. Getting ready
    30. How to do it…
    31. How it works…
    32. There's more…
    33. Creating a hero view transition with .matchedGeometryEffect
    34. Getting ready
    35. How to do it…
    36. How it works…
    37. Creating an animated pressable button
    38. Getting ready
    39. How to do it…
    40. How it works…
    41. Lottie animations in SwiftUI
    42. Getting ready
    43. How to do it…
    44. How it works…
    45. Implementing a stretchable header in SwiftUI
    46. Getting ready
    47. How to do it…
    48. How it works…
    49. Creating floating hearts in SwiftUI
    50. Getting ready
    51. How to do it…
    52. How it works…
    53. See also
    54. How to implement a swipeable stack of cards in SwiftUI
    55. Getting ready
    56. How to do it…
    57. How it works…
  15. Chapter 8: Driving SwiftUI with Data
    1. Technical requirements
    2. Using @State to drive Views behavior
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. See also
    7. Using @Binding to pass a state variable to child Views
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. Implementing a CoreLocation wrapper as @ObservedObject
    12. Getting ready
    13. How to do it…
    14. How it works…
    15. Sharing state objects with multiple views using @EnvironmentObject
    16. Getting ready
    17. How to do it…
    18. How it works…
    19. See also
  16. Chapter 9: Driving SwiftUI with Combine
    1. Technical requirements
    2. Introducing Combine in a SwiftUI project
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. See also
    7. Managing the memory in Combine to build a timer app
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. Validating a form using Combine
    12. Getting ready
    13. How to do it…
    14. How it works…
    15. There's more…
    16. Fetching remote data using Combine and visualizing it in SwiftUI
    17. Getting ready
    18. How to do it…
    19. How it works…
    20. There's more…
    21. Debugging an app based on Combine
    22. Getting ready
    23. How to do it…
    24. How it works…
    25. There's more…
    26. Unit testing an app based on Combine
    27. Getting ready
    28. How to do it…
    29. How it works…
  17. Chapter 10: Handling Authentication and Firebase with SwiftUI
    1. Technical requirements
    2. Implementing SwiftUI Sign in with Apple
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. Implementing UIKit Sign in with Apple to be used in SwiftUI
    7. Getting ready
    8. How to do it…
    9. How it works…
    10. Integrating Firebase into a SwiftUI project
    11. Getting ready
    12. How to do it…
    13. How it works…
    14. There's more…
    15. Using Firebase to sign in using Google
    16. Getting ready
    17. How to do it…
    18. How it works…
    19. Implementing a distributed Notes app with Firebase and SwiftUI
    20. Getting ready
    21. How to do it…
    22. How it works…
    23. There's more…
  18. Chapter 11: Handling Core Data in SwiftUI
    1. Technical requirements
    2. Integrating Core Data with SwiftUI
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. Showing Core Data objects with @FetchRequest
    7. Getting ready
    8. How to do it…
    9. How it works…
    10. Adding Core Data objects to a SwiftUI view
    11. Getting ready
    12. How to do it…
    13. How it works…
    14. Filtering Core Data requests using a predicate
    15. Getting ready
    16. How to do it…
    17. How it works…
    18. Deleting Core Data objects from a SwiftUI view
    19. Getting ready
    20. How to do it…
    21. How it works…
  19. Chapter 12: Cross-Platform SwiftUI
    1. Technical requirements
    2. Creating an iOS app in SwiftUI
    3. Getting ready
    4. How to do it
    5. How it works
    6. Creating the macOS version of the iOS app
    7. Getting ready
    8. How to do it
    9. How it works
    10. Creating the watchOS version of the iOS app
    11. Getting ready
    12. How to do it
    13. How it works
  20. Chapter 13: SwiftUI Tips and Tricks
    1. Technical requirements
    2. Snapshot testing SwiftUI views
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. Unit testing SwiftUI with ViewInspector
    7. Getting ready
    8. How to do it
    9. How it works
    10. Implementing a multilanguage app with localized strings in SwiftUI
    11. Getting ready
    12. How to do it
    13. How it works
    14. Showing a PDF in SwiftUI
    15. Getting ready
    16. How to do it
    17. How it works
    18. There's more
    19. Embedding a MapView in SwiftUI
    20. Getting ready
    21. How to do it
    22. How it works
    23. Embedding a UIKit MapView in SwiftUI
    24. Getting ready
    25. How to do it
    26. How it works
    27. Implementing SwiftUI views using Playground
    28. Getting ready
    29. How to do it
    30. How it works
    31. Using custom fonts in SwiftUI
    32. Getting ready
    33. How to do it
    34. How it works
    35. Implementing asynchronous images in SwiftUI
    36. Getting ready
    37. How to do it
    38. How it works
    39. There's more
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think