Home > AI > IOS > SwiftUI >

ViewBuilder

import SwiftUI

struct TestView: View {
    @State private var showNotification = false

    var body: some View {
        VStack {
            if self.showNotification {
                NotificationView {
                    Text("notification")
                }
            }

            Spacer()

            Button("toggle") {
                self.showNotification.toggle()
            }

            Spacer()
        }
    }
}




struct NotificationView<Content: View>: View {
    let content: Content

    init(@ViewBuilder content: () -> Content) {
        self.content = content()
    }

    var body: some View {
        content
            .padding()
            .background(Color.red)
            .cornerRadius(16)
            .transition(.move(edge: .top))
            .animation(.spring())
    }
}

Leave a Reply