SwiftUI Codes
Best SwiftUI Code Warehouse

Lesson 1: Introduction to SwiftUI

Objective:

By the end of this lesson, you will understand what SwiftUI is, its advantages, and how it differs from UIKit. You will also create your first SwiftUI project and get familiar with its basic structure and lifecycle.

Content:

1. What is SwiftUI?

SwiftUI is Appleā€™s modern framework for building user interfaces across all Apple platforms using a declarative Swift syntax.

2. SwiftUI vs. UIKit

SwiftUI uses a declarative style, whereas UIKit uses an imperative style. Here is a comparison:

UIKit Example:

let label = UILabel()
label.text = "Hello, World!"
view.addSubview(label)

SwiftUI Example:

Text("Hello, World!")

Explanation:

3. Creating a SwiftUI Project

Follow these steps to create a SwiftUI project:

4. Understanding the Basic Structure

The initial project structure includes ContentView.swift and YourAppApp.swift.

ContentView Structure:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, World!")
            .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

This code creates a simple view displaying "Hello, World!" with padding around it.

Explanation:

5. Hello, World! Example

Let's create a more detailed example:

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Text("Hello, World!")
                .padding()
            Text("Welcome to SwiftUI")
                .font(.headline)
                .foregroundColor(.blue)
            Spacer()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Explanation:

Lesson 2: Basic Views with SwiftUI