How can I get started with SwiftUI? | SwiftUI Tutorial | Chapter 1

Ozan Emre Duran
AppleCode Chronicles
3 min readMar 30, 2023

SwiftUI is a powerful toolkit for building user interfaces for Apple platforms. In this tutorial, we will cover the basics of SwiftUI and build a simple app.

Prerequisites

Before we begin, you will need the following:

  • A Mac running macOS 10.15 or later
  • Xcode 11 or later

Creating a new project

  1. Open Xcode and select “Create a new Xcode project”
  2. Select “iOS” as the platform and “Single View App” as the template
  3. Enter a product name for your app and click “Next”
  4. Choose a location to save your project and click “Create”

Creating a user interface

  1. Open the file “ContentView.swift”
  2. Replace the contents of the file with the following code:
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, SwiftUI!")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
  1. Save the file
  2. Run the app by clicking the “Play” button in the top-left corner of Xcode

Adding interactivity

  1. Open the file “ContentView.swift”
  2. Replace the contents of the file with the following code:
import SwiftUI
struct ContentView: View {
@State var name: String = ""
var body: some View {
VStack {
TextField("Enter your name", text: $name)
.padding()
Text("Hello, \\\\(name)!")
.padding()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
  1. Save the file
  2. Run the app by clicking the “Play” button in the top-left corner of Xcode
  3. Enter your name in the text field and see the greeting update in real-time

Using modifiers

Modifiers are one of the key features of SwiftUI. They allow you to modify the appearance and behavior of UI elements without having to write a lot of code. Let’s modify our text field to have a rounded border and a blue background.

  1. Open the file “ContentView.swift”
  2. Modify the contents of the file with the following code:
import SwiftUI
struct ContentView: View {
@State var name: String = ""
var body: some View {
VStack {
TextField("Enter your name", text: $name)
.padding()
.background(Color.blue)
.cornerRadius(10)
Text("Hello, \\\\(name)!")
.padding()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
  1. Save the file
  2. Run the app by clicking the “Play” button in the top-left corner of Xcode

Using lists

Lists are a common UI element in many apps. They allow you to display a collection of items in a scrollable view. Let’s create a list of names using SwiftUI.

  1. Open the file “ContentView.swift”
  2. Modify the contents of the file with the following code:
import SwiftUI
struct ContentView: View {
@State var names: [String] = ["Alice", "Bob", "Charlie"]
var body: some View {
List(names, id: \\\\.self) { name in
Text(name)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
  1. Save the file
  2. Run the app by clicking the “Play” button in the top-left corner of Xcode

Conclusion

In this tutorial, we covered the basics of SwiftUI and built a simple app with a user interface and interactivity. With its declarative syntax and powerful controls, SwiftUI makes it easy to build complex user interfaces for Apple platforms. We also learned how to use modifiers to modify the appearance and behavior of UI elements, and how to use lists to display a collection of items. SwiftUI provides an easy and intuitive way of building complex user interfaces for Apple devices. With its declarative syntax, multiple platform support, and built-in controls, SwiftUI is a must-have toolkit for any iOS or macOS developer.

--

--

Ozan Emre Duran
AppleCode Chronicles

I'm a passionate programmer who loves exploring and using Apple products. Swift is my language of choice.