How to Trigger an Action When a SwiftUI Toggle Changes

Sometimes we may need to call an action every time a toggle changes instead of relying purely on a state property to keep our toggle up to date. Luckily this is easy to do. An action can be called when a toggle is updated (or any control that takes in a binding, such as a slider) and a state property will be used to display the state in the control....

August 11, 2022 · 2 min

How to Add a Search Bar to a List in SwiftUI

SwiftUI now has the ability to add a search bar in iOS 15. This is done through the searchable() modifier which adds a search bar under your navigation view automatically. To get started, we need our data and a state property to keep track of the search term. @State private var searchTerm = "" private let cats = ["Persian", "Tabby", "Ragdoll", "Sphynx", "Maine Coon"] Next, we need a computed property to filter our data depending on the search term....

June 8, 2022 · 2 min