IOS TOPICS

IOS Introduction
Swift UI Label
Swift UI Button
Swift UI Text Field
Swift UI Slider
Swift UI Switch
Swift UI Stepper
Swift UI Date Picker
Swift UI Segmented Control
Views
Table View
Collection Views
SCROLL VIEW
iOS Content View
iOS View Controllers
iOS Bar interface
iOS Navigation interface
iOS Architect Pattern
iOS Libraries
iOS Web request and parsing
iOS user defaults
iOS Coredata and database

Creating a simple toggle


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

ios output

On State

ios output

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:

ios 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:

ios output