SwiftMessages Documentation

repository·master·Indexed 27 days ago

https://github.com/swiftkickmobile/swiftmessages

A highly customizable presentation library for iOS developers to display messages and alerts in UIKit and SwiftUI applications. Features include flexible placement (top, bottom, center), interactive dismiss gestures, background dimming modes, and a built-in queueing system. Supports custom views via MessageView subclassing or SwiftUI view modifiers, keyboard avoidance through KeyboardTrackingView, and modal presentations using SwiftMessagesSegue.

Tokens
3.2K
Snippets
8
Records
20
Agent score
43%

What's inside SwiftMessages

  1. Overview of SwiftMessages

    master

    SwiftMessages is a flexible library for presenting views and view controllers in both UIKit and SwiftUI.

    Key Features:

    • Placement: Display messages at the top, bottom, center, or behind navigation and tab bars.
    • Interactivity: Supports interactive dismiss gestures, including physics-based animations.
    • Customization:
      • Use included nib files and modify them.
      • Subclass MessageView to add custom elements.
      • Supply arbitrary View (SwiftUI) or UIView (UIKit) instances.
    • Visuals: Multiple background dimming modes and various built-in layouts and themes.

    Note: For SwiftUI support in Xcode 16+, you must use version 10.0.2 or later.

  2. Dismiss messages in SwiftUI

    master

    There are three ways to dismiss messages from within SwiftUI:

    1. Via Binding: For messages shown with .swiftMessage(), set the state variable to nil.
    2. Via Environment Action: Use @Environment(\.swiftMessagesHide) private var hide and call hide(animated: true) inside your view.
    3. Direct API: Call SwiftMessages.hide() directly.
  3. Install SwiftMessages manually

    master

    To install SwiftMessages manually:

    1. Place the SwiftMessages repository within your project directory.
    2. In Xcode, add SwiftMessages.xcodeproj to your project.
    3. Configure your app's target:
      • Add the SwiftMessages framework as an embedded binary on the General tab.
      • Add the SwiftMessages framework as a target dependency on the Build Phases tab.
  4. Set the size of a SwiftMessagesSegue

    master

    By default, SwiftMessagesSegue provides device-based sizing (iPad width is capped at 500pt). To specify a custom size, use one of these methods:

    1. Define width and height constraints within the destination view controller.
    2. Set the preferredContentSize property on the destination view controller.
    3. Add explicit width and/or height constraints to segue.messageView.backgroundView.
  5. Install SwiftMessages via Swift Package Manager

    master

    To install SwiftMessages using Swift Package Manager (SPM), follow these steps in Xcode:

    1. Go to File | Swift Packages | Add Package Dependency....
    2. Search for SwiftMessages.
    3. If multiple results appear, ensure you select the one owned by SwiftKick Mobile.
  6. Implement Keyboard Avoidance with KeyboardTrackingView

    master

    To make the message view slide up when the keyboard appears, assign a KeyboardTrackingView to the segue's keyboardTrackingView property.

    To use KeyboardTrackingView independently:

    1. Pin the KeyboardTrackingView to the bottom, leading, and trailing edges of the screen.
    2. Pin the bottom of your content to the top of the KeyboardTrackingView using an equality or inequality constraint.
    segue.keyboardTrackingView = KeyboardTrackingView()
  7. Use SwiftMessages in SwiftUI

    master

    SwiftUI integration is supported via two main approaches:

    1. Manual Presentation

    Wrap a SwiftUI view in a MessageHostingView and call SwiftMessages.show(view:) from an action.

    Use the .swiftMessage(message:content:) view modifier. This behaves similarly to .sheet().

    If your data model conforms to MessageViewConvertible, you can use a simplified version of the modifier without a view builder.

  8. Present a controller on top of all controllers

    master
    If you do not have a specific presenter or want to present a message controller independently, you can pass a WindowViewController as the source argument. This allows the message to be shown in the current window scene at .normal window level. You can customize the presentationContext using SwiftMessages.Config.
  9. Implement Keyboard Avoidance

    master

    Use KeyboardTrackingView to make message views slide up when the keyboard appears.

    To implement this in your app:

    1. Pin a KeyboardTrackingView instance to the bottom, leading, and trailing edges of the screen.
    2. Pin the bottom of your content (that should avoid the keyboard) to the top of the KeyboardTrackingView using an equality or inequality constraint.
    var config = SwiftMessages.defaultConfig
    config.keyboardTrackingView = KeyboardTrackingView()
  10. Configure Accessibility for custom views

    master

    SwiftMessages provides built-in VoiceOver support. To enhance it for custom views:

    • Prefixing: Set MessageView.accessibilityPrefix to provide context (e.g., view.accessibilityPrefix = "warning"). This text is prepended to the combined title and body announcement.
    • Dim Mode: If using config.dimMode, elements below the dim view are non-focusable. If config.dimMode.interactive == true, the dim view becomes focusable and reads "dismiss". Customize this label using config.dimModeAccessibilityLabel.
    • Custom Views: Implement the AccessibleMessage protocol to provide proper accessibility support for non-standard layouts.