1
2
3
4
5
6
7
import SwiftUI
struct MyNewView: View {
var body: some View {
Text("Hello, world!")
.padding()
}
}
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()
}
}
views that implements the above example with the following contents. viewsApp
1
2
3
4
5
6
7
8
9
10
import SwiftUI
@main
struct viewsApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
ContentView
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import SwiftUI
struct ContentView: View {
@State private var showProfile = false
var body: some View {
if showProfile {
UserProfileView()
Button("Back") {
showProfile = false
}
} else {
Button("Profile") {
showProfile = true
}
}
}
}
#Preview {
ContentView()
}
Views
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import SwiftUI
struct UserProfileView: View {
var body: some View {
VStack {
UserAvatarView()
UserNameView()
UserBioView()
}
}
}
struct UserAvatarView: View {
var body: some View {
Image(systemName: "person.circle.fill")
.resizable()
.frame(width: 100, height: 100)
}
}
struct UserNameView: View {
var body: some View {
Text("Golden Rams")
.font(.title)
}
}
struct UserBioView: View {
var body: some View {
Text("I love SwiftUI!")
.font(.subheadline)
}
}
NavigationView.ContentView with the following:
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
}
}
}
}
}
Add Package Views:
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)
}
}
Modify Views:
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)
}
}
Profile button