Swift / Swift UI Basics

Creating Interactive User Interfaces with SwiftUI

This tutorial will guide you in creating interactive UIs with SwiftUI. You will learn how to use different SwiftUI features to create UIs that respond to user inputs.

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Introduces SwiftUI and building modern UI applications using declarative syntax.

1. Introduction

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:

  • Understand the basics of SwiftUI
  • Use different SwiftUI features to create interactive UIs
  • Create and manage complex UI layouts

For this tutorial, you should have a basic understanding of Swift programming. Familiarity with Apple's Xcode IDE is also helpful, but not mandatory.

2. Step-by-Step Guide

2.1 SwiftUI Basics

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.

2.2 Views and Modifiers

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.

3. Code Examples

3.1 Creating a Button

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.

3.2 Using a Slider

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.

4. Summary

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.

5. Practice Exercises

5.1 Exercise 1

Create a Toggle that changes the color of a Text view.

5.2 Exercise 2

Create a TextField and a Text view. The Text view should display whatever you type in the TextField.

5.3 Exercise 3

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:

5.4 Solution 1

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()
        }
    }
}

5.5 Solution 2

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()
        }
    }
}

5.6 Solution 3

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()
        }
    }
}

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Random String Generator

Generate random alphanumeric strings for API keys or unique IDs.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

Age Calculator

Calculate age from date of birth.

Use tool

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help