SwiftUIPager Documentation

repository·main·Indexed 23 days ago

https://github.com/fermoya/swiftuipager

A SwiftUI-native library providing a customizable Pager component for scrollable, page-based interfaces such as carousels or onboarding screens. It features efficient memory management through page recycling and supports vertical or horizontal paging, animated pagination, and endless looping. Compatible with iOS 13.0+, macOS 10.15+, watchOS 6.0+, and tvOS 13.0+.

Tokens
2.6K
Snippets
11
Records
23
Agent score
81%

What's inside SwiftUIPager

  1. Overview of SwiftUIPager

    main

    SwiftUIPager provides a Pager component built with native SwiftUI components. It renders a scrollable container that displays a handful of pages. To optimize memory usage, pages are recycled on scroll, meaning the component only loads enough items to maintain a smooth scrolling experience.

    Key features include:

    • Vertical or horizontal paging.
    • Card alignment and direction control.
    • Animated pagination.
    • Support for various orientations and directions.
  2. Install SwiftUIPager via Swift Package Manager

    main

    In Xcode, follow these steps:

    1. Go to FileSwift PackagesAdd Package Dependency...
    2. Enter the repository URL: https://github.com/fermoya/SwiftUIPager.git
    https://github.com/fermoya/SwiftUIPager.git
  3. Install SwiftUIPager manually

    main
    1. Download the SwiftUIPager.xcframework.
    2. Create a group named Frameworks inside your Xcode project and drag and drop SwiftUIPager.xcframework into it.
    3. In your target's Build Phases, ensure the option Embed & Sign is selected for the framework.
  4. Use SwiftUIPager in legacy projects

    main

    If your application does not meet the standard system version requirements, you can still use SwiftUIPager by following these steps:

    1. Install the framework using the legacy-projects branch (see specific installation methods below).
    2. In your target's Link Binary With Libraries phase, add SwiftUI and mark it as optional.
    3. Wrap all references to Pager in availability checks to ensure compatibility with older OS versions.

    Example:

    if #available(iOS 13, *) {
        Pager(selection: $selection) { ... }
    } else {
        // Fallback for older iOS versions
    }
  5. Install SwiftUIPager via CocoaPods for legacy projects

    main

    To install the legacy-compatible version of SwiftUIPager using CocoaPods, add the following line to your Podfile, specifying the legacy-projects branch:

    pod 'SwiftUIPager', :git => 'https://github.com/fermoya/SwiftUIPager.git', :branch => 'legacy-projects'
  6. Initialize a Pager

    main

    To create a Pager, you must provide a Page object (wrapped in @StateObject or @ObservedObject), an array of data, a KeyPath for identification, and a ViewBuilder to define the page content. Use Page.firstPage() or Page.withIndex(_:) to initialize the page state.

    @StateObject var page: Page = .first()
    var items = Array(0..<10)
    
    var body: some View {
        Pager(page: page,
              data: items,
              id: \.self,
              content: { index in
                  Text("Page: \(index)")
         })
     }
  7. Install SwiftUIPager via Swift Package Manager for legacy projects

    main

    To use the legacy-compatible version with Swift Package Manager (SPM):

    1. Go to FileSwift PackagesAdd Package Dependency...
    2. Enter the URL: https://github.com/fermoya/SwiftUIPager.git
    3. IMPORTANT: When prompted to select a rule/branch, select the legacy-projects branch.
  8. Known issues in SwiftUIPager

    main

    Users may encounter the following issues depending on their environment:

    • NavigationLink and Button conflicts: In SwiftUI 1.0 and iOS 13, NavigationLink and Button might behave unexpectedly if pagingPriority(.simultaneous) is used. This issue is reportedly not reproducible in iOS 14 beta.
    • Precondition failure: Depending on the Xcode version, a precondition failure may occur affecting SwiftUI 1.0 and iOS 13. This does not occur on Xcode 12 beta.