/

SwiftUI Forms: Toggle

SwiftUI Forms: Toggle

In this blog post, we will explore another essential form control in SwiftUI: the Toggle. The Toggle is commonly used in applications like the Settings app. It allows users to switch between two states.

Let’s take a look at an example of how to use the Toggle control:

1
2
3
4
5
6
7
8
9
struct ContentView: View {
@State private var enabled = true

var body: some View {
Form {
Toggle("Enable?", isOn: $enabled)
}
}
}

In the above code snippet, we have a Toggle view inside a Form. The Toggle displays a label “Enable?” and is bound to the enabled state variable using the isOn parameter.

When the enabled state variable is set to true, the toggle is enabled, and when it is set to false, it is disabled. The user can interact with the toggle by tapping on it, and the enabled state variable will automatically update accordingly.

Here’s a visual representation of the Toggle control:

Toggle Control Enabled

Toggle Control Disabled

Using the Toggle control, you can easily implement switches or on/off functionality in your SwiftUI applications.

tags: [“SwiftUI”, “Toggle”, “Form Control”]