No history yet

Declarative UI Fundamentals

What is SwiftUI?

SwiftUI is Apple's modern framework for building user interfaces on all its platforms, from iOS and macOS to watchOS. Instead of writing detailed, step-by-step instructions to draw every button and label, you describe what you want the interface to look like. The framework handles the rest.

SwiftUI is an innovative, exceptionally simple way to build user interfaces across all Apple platforms with the power of Swift

This approach is called declarative UI. It’s a fundamental shift from the older, imperative style you might be used to with frameworks like UIKit.

Declarative vs. Imperative

Think of it like giving directions. The imperative approach is like telling a driver, "Drive two blocks, turn left at the gas station, go three more blocks, and the destination is on your right." You provide a sequence of commands to change the state.

With a declarative approach, you simply show the driver a picture of the destination and say, "Here’s where we need to be." The driver—or in our case, SwiftUI—figures out the most efficient way to get there. You declare the desired end state, and the system makes it happen.

In SwiftUI, your code becomes a blueprint for the UI. You describe how the interface should look based on its current data, or 'state'. When the state changes, SwiftUI automatically and efficiently updates the UI to match.

This simplifies your code by removing the need to manually manage UI updates, which was a common source of bugs in like UIKit.

The View Protocol

Everything you see on screen in a SwiftUI app is a view. From simple text labels to complex list layouts, they are all built from components that conform to the View protocol. This protocol is the essential building block of any SwiftUI interface.

To create a custom view, you define a structure that conforms to the View protocol. The only requirement is to implement a computed property called body. This property describes the view's content.

```swift
// A custom view structure
struct MyFirstView: View {
    // The body property returns the view's content.
    // 'some View' means it will return some concrete type
    // that conforms to the View protocol.
    var body: some View {
        Text("Hello, SwiftUI!")
    }
}

The body property itself returns some View. This opaque return type means you're promising to return something that is a view, without having to specify the exact, often complex, type that SwiftUI creates behind the scenes. It keeps your code clean and focused on structure.

Basic Building Blocks

SwiftUI provides a rich library of primitive views to build your interface. The two most fundamental are Text and Image.

Text

noun

A view that displays one or more lines of read-only text.

The Text view is straightforward. You initialize it with a string, and it displays that string on the screen. You can customize its appearance using modifiers—special methods that create a new, modified version of the view.

struct ContentView: View {
    var body: some View {
        Text("This is a heading")
            .font(.largeTitle) // A modifier to change the font
            .foregroundColor(.blue) // A modifier to change the color
    }
}

Next is the Image view, used for displaying graphics. You typically initialize it with the name of an image asset you've added to your project's asset catalog.

struct ProfileView: View {
    var body: some View {
        Image("profile-picture") // "profile-picture" is in the asset catalog
            .resizable() // Allows the image to be resized
            .aspectRatio(contentMode: .fit) // Maintains aspect ratio
            .frame(width: 100, height: 100) // Constrains the size
            .clipShape(Circle()) // Clips the image into a circle
    }
}
Lesson image

By combining these simple views and applying modifiers, you compose your entire UI. This compositional approach, starting with small, reusable pieces, is central to building apps with SwiftUI.

Quiz Questions 1/5

Which statement best describes the declarative approach to UI development used by SwiftUI?

Quiz Questions 2/5

In SwiftUI, what is the fundamental protocol that all components of a user interface must conform to?

These are the first steps into a declarative way of thinking about UI. Next, we'll explore how to arrange these views into more complex layouts.