SwiftUI forms: Toggle

Another common form control is Toggle.

You can see it widely used in the Settings app.
struct ContentView: View {
@State private var enabled = true

var body: some View {
Form {
Toggle("Enable?", isOn: …


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Another common form control is Toggle.

You can see it widely used in the Settings app.

struct ContentView: View {
    @State private var enabled = true
    
    var body: some View {
        Form {
            Toggle("Enable?", isOn: $enabled)
        }
    }
}

It works similarly to a TextField view, except instead of a String value passed with the text parameter, we pass a Bool value to isOn.

If you set it to true the toggle is enabled, if you set it to false it’s disabled:

Of course the user can enable or disable it by tapping on the control, and that automatically updates the variable passed to isOn.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-09-26T05:00:00+00:00) SwiftUI forms: Toggle. Retrieved from https://www.scien.cx/2021/09/26/swiftui-forms-toggle/

MLA
" » SwiftUI forms: Toggle." flaviocopes.com | Sciencx - Sunday September 26, 2021, https://www.scien.cx/2021/09/26/swiftui-forms-toggle/
HARVARD
flaviocopes.com | Sciencx Sunday September 26, 2021 » SwiftUI forms: Toggle., viewed ,<https://www.scien.cx/2021/09/26/swiftui-forms-toggle/>
VANCOUVER
flaviocopes.com | Sciencx - » SwiftUI forms: Toggle. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/26/swiftui-forms-toggle/
CHICAGO
" » SwiftUI forms: Toggle." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/09/26/swiftui-forms-toggle/
IEEE
" » SwiftUI forms: Toggle." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/09/26/swiftui-forms-toggle/. [Accessed: ]
rf:citation
» SwiftUI forms: Toggle | flaviocopes.com | Sciencx | https://www.scien.cx/2021/09/26/swiftui-forms-toggle/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.