Mastering SwiftUI: The Complete Guide (PART-1)

What is SwiftUI

Key features of SwiftUI

Declarative Syntax:

Text("Hello, SwiftUI!")
.font(.largeTitle)
.foregroundColor(.blue)

Real-Time Previews in Xcode:

Cross-Platform Development:

struct UniversalView: View {
    var message: String  // Make the message customizable

    var body: some View {
        Text(message)
            .font(.headline)
            .padding()
            .frame(maxWidth: .infinity, alignment: .center)  // Make it flexible and center-aligned
    }
}

struct ContentView: View {
    var body: some View {
        UniversalView(message: "Hello, Multi-Platform!")  // Passing custom message
    }
}

Dynamic and Adaptive Design:

struct LabeledContentView: View {
    var body: some View {
        VStack {
            Text("title")
                .font(.largeTitle)
                .padding(.bottom, 5)
            Text("subtitle")
                .font(.subheadline)
                .foregroundColor(.gray)
            Spacer()
        }
        .padding()
    }
}

Reactive Programming with SwiftUI:

struct CounterView: View {
    @State private var count = 0  // State variable to track the count

    var body: some View {
        VStack {
            // Display the current count, fixed string interpolation error
            Text("Count: \(count)")
                .font(.title)
                .padding()

            Button(action: {
                count += 1  // Increment the count when the button is pressed
            }) {
                Text("Increment")
                    .font(.headline)
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }
            .padding()  // Add padding around the button
        }
        .padding()  // Add padding around the VStack
    }
}

Less Code, More Productivity:

Custom Animations aur Transitions:

Why Swift UI?

Swift UI App Lifecycle

SwiftUI App Lifecycle Steps

App Initialization

App Active

App Background

App Termination

App Lifecycle Code Example in SwiftUI

Define the App Lifecycle using @main

import SwiftUI

@main
struct MyApp: App {
// App state properties can be initialized here
@StateObject private var settings = AppSettings()
var body: some Scene {
    WindowGroup {
        ContentView()
            .environmentObject(settings)  // Passing data to all views
    }
  }
}

App Lifecycle Events

var body: some View {
    VStack {
        Text("Welcome to the app!")
            .onAppear {
                print("ContentView appeared!")
                settings.loadUserSettings()
            }
            .onDisappear {
                print("ContentView disappeared!")
                settings.saveUserSettings()
            }

        Button("Tap me") {
            print("Button tapped!")
        }
    }
    .padding()
}

Handling App Background and Termination

@main
struct MyApp: App {
    @Environment(\.scenePhase) private var scenePhase

    var body: some Scene {
        WindowGroup {
            ContentView()
                .onChange(of: scenePhase) { phase in
                    switch phase {
                    case .active: print("App is active.")
                    case .inactive: print("App is inactive.")
                    case .background: print("App is in background.")
                    @unknown default: print("Unknown state.")
                    }
                }
        }
    }
}

App Termination

Conclusion

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *