Swift MIT

PagingMenu

A paging menu controller built from other view controllers placed inside a scroll view

I

iLiuChang

Dernière activité 13 juil. 2026
iLiuChang/PagingMenu

167

étoiles

3

forks

0

issues ouvertes

Ce README est souvent en anglais.

PagingMenu

Swift Platform Version License SPM CocoaPods

A lightweight paging menu controller for iOS. Place view controllers or views inside a horizontal scroll view, with a customizable top bar for switching pages.

Features

  • Horizontal paging with a top menu bar
  • Supports UIViewController and UIView as page content
  • Flexible bar items: String, PagingBarItemTitle, PagingBarItemAttributedTitle, or custom PagingBarItemProvider
  • Custom selected indicator / background via barItemSelectedBackgroundView
  • Bar alignment: leading / center / trailing

Requirements

  • iOS 11.0+
  • Swift 5.0+

Installation

CocoaPods

pod 'PagingMenu'

Then run:

pod install

Swift Package Manager

In Xcode: File → Add Packages… and enter:

https://github.com/iLiuChang/PagingMenu.git

Or add it to your Package.swift:

dependencies: [
    .package(url: "https://github.com/iLiuChang/PagingMenu.git", from: "2.0.0")
]

Manual

  1. Download the files in the Sources folder
  2. Drag them into your Xcode project

Usage

override func viewDidLoad() {
    super.viewDidLoad()

    let pagingMenu = PagingMenuController()
    pagingMenu.barHeight = 44
    pagingMenu.barInset = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
    pagingMenu.barItemNormalStyle = PagingBarItemStyle(
        color: .black.withAlphaComponent(0.5),
        font: UIFont.systemFont(ofSize: 16)
    )
    pagingMenu.barItemSelectedStyle = PagingBarItemStyle(
        color: .black,
        font: UIFont.systemFont(ofSize: 16)
    )
    pagingMenu.items = (
        ["Title1", "Title2", "Title3"],
        [UIViewController(), UIViewController(), UIViewController()]
    )

    // Also supports UIView:
    // pagingMenu.items = (["Title1", "Title2", "Title3"], [UIView(), UIView(), UIView()])

    // Mix UIView and UIViewController:
    // pagingMenu.items = (["Title1", "Title2", "Title3"], [UIView(), UIViewController(), UIView()])

    addChild(pagingMenu)
    view.addSubview(pagingMenu.view)
    pagingMenu.view.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
        pagingMenu.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        pagingMenu.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        pagingMenu.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        pagingMenu.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ])
    pagingMenu.didMove(toParent: self)
}

Selected indicator

Use barItemSelectedBackgroundView to show a line (or any custom view) under the selected title:

let itemBg = UIView()
let line = UIView()
line.backgroundColor = .blue
line.layer.cornerRadius = 1.5
itemBg.addSubview(line)
line.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    line.bottomAnchor.constraint(equalTo: itemBg.bottomAnchor),
    line.centerXAnchor.constraint(equalTo: itemBg.centerXAnchor),
    line.widthAnchor.constraint(equalToConstant: 16),
    line.heightAnchor.constraint(equalToConstant: 3)
])
pagingMenu.barItemSelectedBackgroundView = itemBg

Bar alignment

The top bar defaults to leading alignment. Center it with:

pagingMenu.barAlignment = .center

Custom bar & container items

Bar items support String, PagingBarItemAttributedTitle, PagingBarItemTitle, or a custom PagingBarItemProvider.
Containers support UIViewController, UIView, or a custom PagingContainerItemProvider.

public protocol PagingBarItemProvider {
    var normalAttributedTitle: NSAttributedString { get }
    var selectedAttributedTitle: NSAttributedString { get }
}

public protocol PagingContainerItemProvider {
    var pagingContainerItemView: UIView { get }
    func addToSuper(_ superView: UIView, pagingMenuController: PagingMenuController)
    func removeFromSuper(_ pagingMenuController: PagingMenuController)
}

Delegate

pagingMenu.delegate = self

func pagingMenuController(
    _ pagingMenuController: PagingMenuController,
    didSelectAt index: Int,
    actionBehavior: PagingMenuController.ActionBehavior
) {
    // actionBehavior: .click or .scroll
}

Example

Open Demo/PagingMenu.xcodeproj and run the demo target.

License

PagingMenu is available under the MIT license. See the LICENSE file for details.

Projets similaires

Swipe-based paging UI for iOS

Swiftanimation-libraryiosswift
Yyysskk
1,3 k étoiles128

iOS嵌套联动分页控制器

Swiftcontainer-viewdynamic-pageshvscrollview
SSPStore
617 étoiles108

📖 A lightweight, paging view solution for SwiftUI

Swiftiospagingswift
Nnachonavarro
875 étoiles64