β Key features of the package:
- Position β Configure the snackbar to appear at the top, center, or bottom of the screen.
- Left Icon β Configure a predefined icon or a custom image.
- Right Action β Set an action with text or an image.
- Decorator β Customize the background color, title and message colors, and the snackbar's maximum width.
- Properties β Adjust the position, duration, and haptic feedback behavior.
Add the following dependency to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/chrisnyw/SwiftUI-Snackbar", from: "1.0.0")
]
Alternatively, add it using Xcode:
- Go to File > Swift Packages > Add Package Dependency
- Enter the URL:
https://github.com/chrisnyw/SwiftUI-Snackbar
- Choose the latest version
Import the package and start using it:
import SwiftUISnackbar
struct SnackbarDemoView: View {
@State private var snackbar: Snackbar?
var body: some View {
Button(
action: { snackbar = Snackbar(title: "title", message: "message") },
label: { Text("Show Snackbar") }
)
.snackbarView(snackbar: $snackbar)
}
}
Here is the full configuration of the Snackbar:
Snackbar(
title: "Demo Full",
message: "Your message goes here!",
properties: Snackbar.Properties(
position: .bottom,
duration: .fixed(seconds: 5),
disableHapticVibration: true
),
icon: .system(imageName: "info.circle.fill", color: .white),
action: .text("Tap me", .red, { print("Tapped me") }),
decorator: Snackbar.Decorator(
backgroundColor: .black,
titleTextColor: .orange,
messageTextColor: .yellow
)
)
This Snackbar is highly customizable. Below is a list of all available configuration options:
Property | Type | Description |
---|---|---|
title |
String? |
Optional title text. Details: Title |
message |
String |
Required message text. Details: Message |
properties |
Properties |
Snackbar properties. Details: Properties |
decorator |
Decorator |
Background and text color settings. Details: Decorator |
icon |
Icon |
Icon on the left side of the snackbar. Details: Icon |
action |
Action |
Action on the right side of the snackbar. Details: Action |
Set an optional title for the Snackbar. If title
is nil
, the message will be center-aligned.
Set the message text for the Snackbar. It will automatically expand to display multiline content if needed.
Property | Type | Description |
---|---|---|
position |
Position |
Configure the snackbar position. See: Position |
duration |
Duration |
Set how long the snackbar will be displayed. See: Duration |
disableHapticVibration |
Bool |
Disable haptic feedback when the snackbar appears |
Case | Parameters | Description |
---|---|---|
fixed |
seconds: TimeInterval |
Display the snackbar for a fixed duration (in seconds) |
infinite |
β | Display the snackbar indefinitely |
Property | Type | Description |
---|---|---|
width |
Double |
Set the snackbar's maximum width |
backgroundColor |
Color |
Set the snackbar's background color |
titleTextColor |
Color |
Set the title's text color |
messageTextColor |
Color |
Set the message's text color |
Define the supported actions. You can capture a callback when the user taps the action button.