In this tutorial, we'll be exploring SwiftUI, Apple's innovative framework that allows developers to design apps in a declarative way. Our focus will be on creating interactive user interfaces (UIs) that respond to user inputs.
By the end of this tutorial, you will be able to:
For this tutorial, you should have a basic understanding of Swift programming. Familiarity with Apple's Xcode IDE is also helpful, but not mandatory.
SwiftUI is a declarative UI framework, which means you describe what your UI should look like, and SwiftUI takes care of rendering it. SwiftUI works across all Apple platforms, making it simple to create robust and beautiful UIs with less code.
In SwiftUI, everything from buttons to screens is a View. Modifiers are methods that you can call on a view to customize its appearance and behavior.
Button(action: {
print("Button tapped!")
}) {
Text("Tap me")
}.padding()
.background(Color.blue)
.foregroundColor(Color.white)
.cornerRadius(10)
In this example, Button and Text are views, while padding(), background(Color.blue), foregroundColor(Color.white) and cornerRadius(10) are modifiers.
Let's start with a simple example of creating a button that changes a text view when tapped.
struct ContentView: View {
@State private var isButtonTapped = false
var body: some View {
VStack {
Text(isButtonTapped ? "Button was tapped!" : "Tap the button")
.font(.title)
.padding()
Button(action: {
isButtonTapped.toggle()
}) {
Text("Tap me")
.padding()
.background(Color.blue)
.foregroundColor(Color.white)
.cornerRadius(10)
}
}
}
}
In this code, @State is a property wrapper that provides a source of truth for data in your app. When the button is tapped, isButtonTapped toggles its value, causing the UI to update.
Now let's create a slider that changes the opacity of an image.
struct ContentView: View {
@State private var opacity = 0.5
var body: some View {
VStack {
Image("swiftui-logo")
.resizable()
.scaledToFit()
.opacity(opacity)
Slider(value: $opacity, in: 0...1)
.padding()
}
}
}
Here, the $ sign is used to create a two-way binding between the slider and the opacity state. When you move the slider, SwiftUI automatically updates the opacity value and re-renders the image.
In this tutorial, we learned about SwiftUI and its declarative syntax. We explored how to create basic interactive elements using @State and two-way bindings, and saw how SwiftUI automatically updates your UI when your app's state changes.
For further learning, I recommend exploring more complex SwiftUI views, such as List, NavigationView, and TabView, and learning about @Binding, @ObservedObject, and @EnvironmentObject for managing more complex state.
Create a Toggle that changes the color of a Text view.
Create a TextField and a Text view. The Text view should display whatever you type in the TextField.
Create a Picker that allows you to choose from a list of options. The chosen option should be displayed in a Text view.
Here are the solutions for the exercises:
struct ContentView: View {
@State private var isSwitchOn = false
var body: some View {
VStack {
Text("Hello, World!")
.font(.largeTitle)
.foregroundColor(isSwitchOn ? Color.green : Color.red)
Toggle(isOn: $isSwitchOn) {
Text("Change color")
}.padding()
}
}
}
struct ContentView: View {
@State private var inputText = ""
var body: some View {
VStack {
TextField("Type something...", text: $inputText)
.padding()
.border(Color.black, width: 0.5)
Text(inputText)
.font(.largeTitle)
.padding()
}
}
}
struct ContentView: View {
let options = ["Option 1", "Option 2", "Option 3"]
@State private var selectedOption = "Option 1"
var body: some View {
VStack {
Picker("Options", selection: $selectedOption) {
ForEach(options, id: \.self) {
Text($0)
}
}.padding()
Text("You selected: \(selectedOption)")
.font(.largeTitle)
.padding()
}
}
}