Home SwiftUI
Post
Cancel

SwiftUI

Overview

You’ve probably never heard of SwiftUI before, so I’ll clear some things up.

  • SwiftUI is a framework, which just means “developer tool.”
  • SwiftUI is built using the Swift programming language, which we’ll go over in the next section.
  • SwiftUI is the equivalent to HTML + CSS – you write and style views using it.

Credit: Apple

In SwiftUI, there are 2 main concepts: Views and View Modifiers. You compose these together to form a user interface (that’s that the “UI” part stands for in SwiftUI). Here’s the basic structure of a view:

1
2
3
4
5
6
7
8
9
10
11
12
13
/// 1.
struct ContentView: View {

    /// 2.
    var body: some View {

        /// 3.
        Text("Hello, world!")

        /// 4.
        .foregroundColor(Color.blue)
    }
}
  1. This declares a custom view with the name “ContentView.”
  2. This is where you put sub-views inside.
  3. This is another view, called Text. The “Hello World” part is a parameter, which we’ll go over later.
  4. This is a view modifier, which is attached to the Text. As you can assume, it changes the text’s color to blue.

Text is a built-in view. There are several dozen more of these views – here’s a list. However, we’ll only focus on a couple.

At its core, that’s all SwiftUI is – a framework to combine and modify views. In the next section, we’ll go over SwiftUI in more detail.

This post is licensed under CC BY 4.0 by the author.

Swift Playgrounds

Challenge #1