Creating a simple toggle
First define a state variable of type “Bool” with a true or false state as required and use the following piece of code.
Code:
struct ContentView: View {
@State private var Message = true
var body: some View {
VStack {
Toggle("Display version information", isOn: $Message)
if Message {
Text("This is Xcode version 12.4!")
}
}
}
}
Output:
Off State
On State
To customise the switch color: Use a toggleStyle() modifier
Code:
VStack {
Toggle("Display version information", isOn: $Message)
.toggleStyle(SwitchToggleStyle(tint: .blue))
if Message {
Text("This is Xcode version 12.4!")
}
}
Output:
Edit the title style of the text using a .text() modifier
Code:
Toggle("Display version information", isOn: $Message)
.toggleStyle(SwitchToggleStyle(tint: .blue))
.font(.title)
Output: