-
Notifications
You must be signed in to change notification settings - Fork 3
Add a Snackbar to your View
Luca Zani edited this page Mar 19, 2021
·
1 revision
To add a Snackbar to your SwiftUI View you must first import the package
import SwiftUISnackbarThen to use it just add to your view the custom View's method .snackbar
Text("Hello, World!")
.snackbar(...)
}To set the values on the Snackbar you can either define them individually like so
import SwiftUI
import SwiftUISnackbar
struct YourView: View {
var title: String = "Snackbar Title"
var text: String = "Hi I'm the Snackbar's text"
@State var show: Bool = true
var body: some View {
Text("Hello, World!")
.snackbar(isShowing: $show, title: title, text: text, style: .custom(.blue))
}
}Or by using the Wrapper SnackbarStore that you can use to interact with the Snackbar
The
SnackbarStorelets you interact with theSnackbarvia some methods as shown in This Page
You can use it as a @StateObject or as an @EnvironmentObject so that you can use it by injecting it in the View Hierarchy making it accessible from any children view
struct MyView: View {
@StateObject var store = SnackbarStore()
var body: some View {
Text("Hello, World!")
.snackbar(isShowing: $store.show, title: store.title, text: store.text, style: store.style, actionText: store.actionText, action: store.action)
}
}Remember to inject it in the View Hierarchy like so
MyView()
.environmentObject(SnackbarStore())And the you can use it normally
struct MyView: View {
@EnvironmentObject var store: SnackbarStore
var body: some View {
Text("Hello, World!")
.snackbar(isShowing: $store.show, title: store.title, text: store.text, style: store.style, actionText: store.actionText, action: store.action)
}
}
For any additional information or problem please contact me at zani.luca@icloud.com