Reader small image

You're reading from  SwiftUI Cookbook - Third Edition

Product typeBook
Published inDec 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781805121732
Edition3rd Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Juan C. Catalan
Juan C. Catalan
author image
Juan C. Catalan

Juan C. Catalan is a software engineer with more than 18 years of professional experience. He started mobile development back in the days of iOS 3. Juan has worked as a professional iOS developer in many industries, including medical devices, financial services, real estate, document management, fleet tracking and industrial automation. He has contributed to more than 30 published apps in the App Store, some of them with millions of users. Juan gives back to the iOS development community with technical talks, mentoring developers, reviewing technical books and now as a book author. He lives in Austin, Texas, with his wife Donna, where they spend time with their kids.
Read more about Juan C. Catalan

Right arrow

Presenting alerts

A common way of showing important information to the users is by using alerts with a message and some buttons. In this recipe, we will create a simple alert that gets displayed when a button is pressed.The way alerts are presented in iOS 13 and 14 has been deprecated. iOS 15 introduced a new way of presenting alerts. For the sake of being able to handle devices on earlier iOS versions, we'll go over both ways of presenting alerts, starting with iOS 15. The code for iOS 13 and 14 alerts is found in the OldAlerts.swift file while the newer iOS 15 version is in ContentView.swift.

Getting ready

Create a new SwiftUI project called PresentingAlerts.Make sure that the project's iOS Deployment Target is at least iOS 15.0.

How to do it…

We will add a Button or Text to the scene that can be used to display an alert containing a single button. The iOS 15 steps are as follows:

  1. Create a @State variable that will be used to trigger the presentation of our Alert...

Adding actions to alert buttons

We may want to display alerts with more than just an OK button to confirm the alert has been read. In some cases, we may wish to present a Yes or No choice to the user. For example, if the user wants to delete an item in a list, we may wish to present an alert that provides the option of whether to proceed with the deletion or cancel the action.In this recipe, we will look at how to add multiple buttons to our alert. We will provide the descriptions for iOS 15 alerts. You can find the code for iOS 13 and 14 alerts in the OlderAlertsWithActions.swift file.

Getting ready

Create a new SwiftUI app called AlertsWithActions.

How to do it…

We will implement an alert with two buttons and an action. The alert will get triggered by a tap gesture on a button. When triggered, the alert displays a message asking the user if they want to change the text currently being displayed. A tap on OK changes the button title, while a tap on Cancel leaves the text unchanged...

Presenting confirmation dialogs

SwiftUI confirmation dialogs and alerts are both used to request additional information from the user by interrupting the normal flow of the app to display a message. Confirmation dialogs give the user some additional choices related to an action that they are currently taking, whereas the Alerts view informs the user if something unexpected happens or they are about to perform an irreversible action.Confirmation dialogs were introduced in iOS 15. The similar but deprecated functionality in iOS 13 and 14 is called ActionSheet. The implementation for ActionSheet is located in the oldActionSheets.swift file.In this recipe, we will create a confirmation dialog that gets displayed when the user taps some text in the view.

Getting ready

Create a new SwiftUI project called PresentingConfirmationDialogs.

How to do it…

We will implement a confirmation dialog by adding the .confirmationDialog(_:isPresented:titleVisibility:actions:) view modifier to a...

Presenting new views using sheets and full-screen covers

Sheets and full-screen covers are another way to present modals in our apps. Sheets are used to present views modally over existing ones while allowing the user to dismiss the sheet by dragging it down. A full-screen cover, or cover for short, is another modal view that takes as much of the screen as possible.In this recipe, we will learn how to present several sheets to the user, how to control the height of a sheet, how to add a navigation bar with a Dismiss button to a sheet, and how to present a full-screen cover.

Getting ready

Create a new SwiftUI project named PresentingSheets.

How to do it…

We will create two SwiftUI views named SheetView and SheetWithNavBar that will be displayed modally when a button is clicked. The steps are as follows:

  1. In ContentView.swift, between the struct declaration and the body variable, add three state variables of type Bool. The state variables will be used to trigger the presentation...

Displaying popovers

A popover is a view that can be displayed on screen to provide more information about a particular item. It includes an arrow pointing to the location where it originates from. You can tap on any other screen area to dismiss the popover. Popovers are typically used on larger screens such as on the iPad.In this recipe, we will create and display a popover on an iPad.

Getting ready

Create a new SwiftUI project named DisplayingPopovers.

How to do it…

Following the pattern we've used so far in this chapter, we will first create a @State variable whose value triggers the displaying or hiding of a popover. Then we will add a .popover() modifier that displays the popover when the @State variable is true. The steps are as follows:

  1. Just above the body variable in the ContentView.swift file, add a state variable that will be used to trigger the display of the popover:
@State private var showPopover = false
  1. Within the body variable, replace the contents with...

Programmatically switching tabs on a TabView

In the preceding recipe, we learned how to switch between tabs by tapping on tab items at the bottom of the screen. However, we may also want to programmatically trigger tab switching. In this recipe, we will use a tap gesture to trigger the transition from one tab to another.

Getting ready

Create a new SwiftUI app named TabViewWithGestures.

How to do it…

We will create a TabView with two items, each containing some text that triggers a tab switch on click. The steps are given here:

  1. Open the ContentView.swift file, and add a @State variable to hold the value of the currently selected tab:
    struct ContentView: View {
        @State private var selectedTab = 0
    ...
    }
    
  2. Replace the Text view in the body variable with a TabView, with two tab items:
        var body: some View {
            TabView(selection: $selectedTab) {
                Text("Left Tab. Tap to switch to right")
         ...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
SwiftUI Cookbook - Third Edition
Published in: Dec 2023Publisher: PacktISBN-13: 9781805121732
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Juan C. Catalan

Juan C. Catalan is a software engineer with more than 18 years of professional experience. He started mobile development back in the days of iOS 3. Juan has worked as a professional iOS developer in many industries, including medical devices, financial services, real estate, document management, fleet tracking and industrial automation. He has contributed to more than 30 published apps in the App Store, some of them with millions of users. Juan gives back to the iOS development community with technical talks, mentoring developers, reviewing technical books and now as a book author. He lives in Austin, Texas, with his wife Donna, where they spend time with their kids.
Read more about Juan C. Catalan