JDStatusBarNotification
repository·main·Indexed 26 days ago
https://github.com/calimarkus/jdstatusbarnotificationA customizable library for displaying notifications below the iOS status bar, notch, or Dynamic Island. It supports pill and full-width layouts, animations (move, fade, bounce), and custom views using SwiftUI or UIKit. Features include state-driven presentation via SwiftUI modifiers, manual control through a singleton presenter, progress bars, activity indicators, and semantic styles for success, warning, and error states.
What's inside JDStatusBarNotification
- JDStatusBarNotification is a highly customizable and feature-rich library for displaying notifications directly below the iOS status bar. It supports various styles including pill shapes, progress bars, and custom left-view configurations.
Present notifications manually (Swift or ObjC)
mainUseNotificationPresenter.sharedto manually trigger notifications from your logic. This is useful for non-SwiftUI contexts or imperative code.Install JDStatusBarNotification
mainYou can install JDStatusBarNotification using Swift Package Manager, CocoaPods, or Carthage.
Swift Package Manager (SPM)
Add the package via Xcode:
Xcode -> File -> Add packages: git@github.com:calimarkus/JDStatusBarNotification.gitImporting:
- In Swift:
import JDStatusBarNotification - In ObjC:
@import JDStatusBarNotification;
CocoaPods
Add the following to your Podfile:
pod 'JDStatusBarNotification'Carthage
Add to your Cartfile:
github "calimarkus/JDStatusBarNotification"Manual Installation
Copy the
JDStatusBarNotification/JDStatusBarNotificationfolder directly into your project.git@github.com:calimarkus/JDStatusBarNotification.git- In Swift:
Use SwiftUI state-driven notifications
mainUse the
.notificationview modifier to drive notification presentation via SwiftUI bindings. This allows you to toggle notifications based on state changes in your views.// Simple text notification var body: some View { Button("Present/dismiss") { isPresented.toggle() } .notification(title: "Hello World", isPresented: $isPresented) } // Styled notification with subtitle, activity, and progress var body: some View { Button("Present/dismiss") { isPresented.toggle() } .notification(title: "A text", subtitle: "with a little subtitle.", isPresented: $isPresented, isShowingActivity: $activity, // toggles an activity indicator on/off progress: $progress, // sets the percentage of a progress bar includedStyle: .success) // picks a predefined style } // Custom SwiftUI view as notification var body: some View { Button("Present/dismiss") { isPresented.toggle() } .notification(isPresented: $isPresented) { Text("👋 Hi there!") .font(.subheadline) .foregroundStyle(.white) } }Present notifications in SwiftUI using state-driven presentation
mainUse the
.notificationview modifier to present notifications based on a SwiftUIBinding<Bool>. This approach allows for declarative control over the notification's visibility and content.Simple text notification
.notification(title: "Hello World", isPresented: $isPresented)Styled notification with subtitle, activity, and progress
.notification(title: "A text", subtitle: "with a little subtitle.", isPresented: $isPresented, isShowingActivity: $activity, progress: $progress, includedStyle: .success)Custom SwiftUI view notification
.notification(isPresented: $isPresented) { Text("👋 Hi there!") .font(.subheadline) .foregroundStyle(.white) }var body: some View { Button("Present/dismiss") { isPresented.toggle() } .notification(title: "Hello World", isPresented: $isPresented) }Troubleshoot: Notifications not appearing in UIWindowScene apps
mainIf your application uses
UIWindowScene, theNotificationPresentermay fail to automatically find the correct window scene, resulting in no notifications being displayed.You can resolve this by explicitly setting the window scene:
NotificationPresenter.shared().setWindowScene(windowScene)Show a styled notification with subtitle, activity, or progress in SwiftUI
mainUse the
.notification(title:subtitle:isPresented:isShowingActivity:progress:includedStyle:)modifier to present more complex notifications. You can include a subtitle, toggle an activity indicator, set a progress bar percentage, and choose a predefined style (e.g.,.success).var body: some View { Button("Present/dismiss") { isPresented.toggle() } .notification(title: "A text", subtitle: "with a little subtitle.", isPresented: $isPresented, isShowingActivity: $activity, // toggles an activity indicator on/off progress: $progress, // sets the percentage of a progress bar includedStyle: .success) // picks a predefined style }Customize notification styles in SwiftUI
mainYou can customize the appearance of a notification directly within a SwiftUI view using the
styleparameter in the.notificationmodifier. This accepts aNotificationStyleClosure..notification(isPresented: $isPresented, style: { let s = $0.backgroundStyle s.backgroundColor = .black s.pillStyle.minimumWidth = 150 s.pillStyle.height = 44 }) { Text("👋 Hi there!") .font(.subheadline) .foregroundStyle(.white) }var body: some View { Button("Present/dismiss") { isPresented.toggle() } .notification(isPresented: $isPresented, style: { let s = $0.backgroundStyle s.backgroundColor = .black s.pillStyle.minimumWidth = 150 s.pillStyle.height = 44 }) { Text("👋 Hi there!") .font(.subheadline) .foregroundStyle(.white) } }Show a simple text notification in SwiftUI
mainUse the
.notification(title:isPresented:)modifier on a SwiftUIViewto present a basic text notification. This modifier is state-driven and requires a binding to a Boolean value to control visibility.var body: some View { Button("Present/dismiss") { isPresented.toggle() } .notification(title: "Hello World", isPresented: $isPresented) }Show a custom view as a notification in SwiftUI
mainUse the
.notification(isPresented:viewBuilder:)modifier to provide a custom SwiftUI view to be displayed inside the notification pill instead of the default text layout.var body: some View { Button("Present/dismiss") { isPresented.toggle() } .notification(isPresented: $isPresented) { Text("👋 Hi there!") .font(.subheadline) .foregroundStyle(.white) } }Modify notification style using a style closure in SwiftUI
mainUse the
styleparameter within the.notificationmodifier to pass aNotificationStyleClosure. This allows you to modify properties likebackgroundStyle.backgroundColororpillStyle.minimumWidthandpillStyle.heightinline.var body: some View { Button("Present/dismiss") { isPresented.toggle() } .notification(isPresented: $isPresented, style: { let s = $0.backgroundStyle s.backgroundColor = .black s.pillStyle.minimumWidth = 150 s.pillStyle.height = 44 }) { Text("Oh hello!") } }Dismiss a notification
mainDismiss the currently visible notification usingdismiss(animated:after:completion:).