JDStatusBarNotification

repository·main·Indexed 26 days ago

https://github.com/calimarkus/jdstatusbarnotification

A 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.

Tokens
4.7K
Snippets
11
Records
33
Agent score
86%

What's inside JDStatusBarNotification

  1. Install JDStatusBarNotification

    main

    You 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.git

    Importing:

    • 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/JDStatusBarNotification folder directly into your project.

    git@github.com:calimarkus/JDStatusBarNotification.git
  2. Use SwiftUI state-driven notifications

    main

    Use the .notification view 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)
        }
    }
  3. Present notifications in SwiftUI using state-driven presentation

    main

    Use the .notification view modifier to present notifications based on a SwiftUI Binding<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)
    }
  4. Troubleshoot: Notifications not appearing in UIWindowScene apps

    main

    If your application uses UIWindowScene, the NotificationPresenter may 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)
  5. Show a styled notification with subtitle, activity, or progress in SwiftUI

    main

    Use 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
    }
  6. Customize notification styles in SwiftUI

    main

    You can customize the appearance of a notification directly within a SwiftUI view using the style parameter in the .notification modifier. This accepts a NotificationStyleClosure.

    .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)
        }
    }
  7. Show a custom view as a notification in SwiftUI

    main

    Use 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)
        }
    }
  8. Modify notification style using a style closure in SwiftUI

    main

    Use the style parameter within the .notification modifier to pass a NotificationStyleClosure. This allows you to modify properties like backgroundStyle.backgroundColor or pillStyle.minimumWidth and pillStyle.height inline.

    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!")
        }
    }