Views and 3rd Party Library

Views

New SwiftUI View
1
2
3
4
5
6
7
import SwiftUI
struct MyNewView: View {
    var body: some View {
        Text("Hello, world!")
            .padding()
    }
}
Breakdown of New SwiftUI View
1
2
import SwiftUI
struct MyNewView: View {
1
2
3
4
var body: some View {
    Text("Hello, world!")
        .padding()
}
1
2
3
4
5
struct ContentView: View {
    var body: some View {
        MyNewView()
    }
}
Structuring Views
NavigationView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct ContentView: View {
    @State private var showProfile = false

    var body: some View {
        NavigationView {
            if showProfile {
                UserProfileView()
                    .navigationBarTitle("User Profile", displayMode: .inline)
                    .navigationBarItems(trailing: Button("Back") {
                        showProfile = false
                    })
            } else {
                Button("Profile") {
                    showProfile = true
                }
            }
        }
    }
}

Third Party Library

Adding third party packages
Example: Kingfisher
1
2
3
4
5
6
7
8
9
import Kingfisher

struct UserAvatarView: View {
    var body: some View {
        KFImage(URL(string: "https://www.wcupa.edu/communications/images/goldenRamLogo.png"))
            .resizable()
            .frame(width: 100, height: 100)
    }
}
Example: ConfettiSwiftUI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import ConfettiSwiftUI

struct UserAvatarView: View {
    @State private var trigger: Int = 0
    var body: some View {
        KFImage(URL(string: "https://www.wcupa.edu/communications/images/goldenRamLogo.png"))
            .resizable()
            .frame(width: 100, height: 100)
        Button("Cheers!!!") {
            trigger += 1
        }
        .confettiCannon(trigger: $trigger)
    }
}
Hands-on: Subsonic