Book Image

SwiftUI Cookbook - Second Edition

By : Giordano Scalzo, Edgar Nzokwe
Book Image

SwiftUI Cookbook - Second Edition

By: Giordano Scalzo, Edgar Nzokwe

Overview of this book

SwiftUI provides an innovative and simple way to build beautiful user interfaces (UIs) for all Apple platforms, from iOS and macOS through to watchOS and tvOS, using the Swift programming language. In this recipe-based cookbook, you’ll cover the foundations of SwiftUI as well as the new SwiftUI 3 features introduced in iOS 15 and explore a range of essential techniques and concepts that will help you through the development process. The cookbook begins by explaining how to use basic SwiftUI components. Once you’ve learned the core concepts of UI development, such as Views, Controls, Lists, and ScrollViews, using practical implementations in Swift, you'll advance to adding useful features to SwiftUI using drawings, built-in shapes, animations, and transitions. 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 by 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 encountered when building SwiftUI apps.
Table of Contents (17 chapters)

Adding buttons and navigating with them

In this recipe, we will learn how to use various buttons available in SwiftUI. We will use a Button view to trigger the change of a count when clicked and implement a NavigationView to move between various SwiftUI views and an EditButton to remove items from a list. We will also briefly discuss the MenuButton and PasteButton only available in macOS.

Getting ready

Let's start by creating a new SwiftUI project called ButtonsApp.

How to do it…

Let's create a home screen with buttons for each of the items we want to go over. Once clicked, we'll use SwiftUI's navigation view to go to the view that implements the clicked concept. The steps are given here:

  1. Add a new SwiftUI view file called ButtonView to the project: File | New | File (or press the shortcut keys + N).
  2. Select SwiftUI View from the UI templates.
  3. In the Save As field of the pop-up menu, enter the filename ButtonView.
  4. Repeat Step...