Swipeable tabs with a bar under the selected tab, the look Tabman gives, take one view controller in UIKit: a horizontal scroll view of tab buttons on top, a UIPageViewController below, and a 2-point view that moves under the selected button. Pass the first tab to show through the initializer, so it's set before the view loads and the screen opens on that tab instead of scrolling to it in front of the user. If you keep Tabman, its data source already has a method for that: defaultPage(for:).
SWIFT TIPS · UIKIT · PAGING
First posted on June 19, 2018, as "[CocoaPods] How to Use TabMan". Rewritten on November 18, 2026. The tabs and the 2018 push order were run on a Mac as a Mac Catalyst program, which runs UIKit on macOS (not an iPhone or the simulator), with Swift 6.2.3 (Xcode 26.2) on macOS 26.0.1, so the sizes below use the Mac's fonts. Tabman wasn't installed; what this post says about it comes from its and Pageboy's source code. The code was type-checked against the iOS 26.2 SDK in Swift 5 and Swift 6 modes with iOS 15 and iOS 26.2 as deployment targets, but not run on a device.
In 2018 I needed a screen with six pages as tabs that you could tap or swipe between, with an indicator line under the current one. The post used Tabman and Pageboy from CocoaPods: a TabmanViewController subclass, an array of six view controllers, bar items with titles, the scrolling button bar style, and an appearance block for the indicator color, weight and font. An update section added a way to open the screen on a chosen tab from a collection view on the previous screen.
That screen is a common pattern. This rewrite builds it with UIKit alone, tests it, and looks closely at the "open on a chosen tab" part, which in 2018 depended on the order of two lines.
Opening on a chosen tab
In the update, tapping a cell pushed the tab screen and then set its index:
self.navigationController?.pushViewController(vc1, animated: true)
vc1.itemSelectIndex = 1
The tab screen read itemSelectIndex in viewDidLoad and called scrollToPage(.at(index: 1), animated: true). Two things about that:
- It works only if the view loads after the second line. I tested the same order with a stand-in screen. In my test the view hadn't loaded right after
pushViewController, so the index was set in time andviewDidLoadsaw 2. But that test had no window, and the order in which UIKit loads a pushed screen's view isn't something to build on. Giving the index to the initializer removes the question. - It scrolls in front of the user.
animated: trueinviewDidLoadstarts on the first tab and then slides to the chosen one. Pageboy's data source hasdefaultPage(for:), which its source describes as the default page to display; the 2018 code returnednilthere. Returning.at(index: itemSelectIndex)opens directly on that page.
The switch in the cell handler also pushed the same screen in every case and only differed in the index, so for rows 0 to 3 one line with indexPath.row would do; rows 4 and up hit default and pushed nothing.
Tabs without a library
import UIKit
/// Tabs at the top that scroll sideways, swipeable pages below, and a bar under the selected tab.
/// The first page to show comes in through init, so it's set before the view loads.
final class PagingTabsViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
private let pages: [UIViewController]
private(set) var selectedIndex: Int
private let tabScroll = UIScrollView()
private let tabStack = UIStackView()
private let indicator = UIView()
private let pageController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal)
private var buttons: [UIButton] = []
init(pages: [UIViewController], initialIndex: Int = 0) {
precondition(!pages.isEmpty, "Give at least one page")
self.pages = pages
self.selectedIndex = min(max(initialIndex, 0), pages.count - 1)
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
// Tabs: one button per page title, in a horizontal scroll view.
tabStack.axis = .horizontal
tabStack.spacing = 20
for (index, page) in pages.enumerated() {
let button = UIButton(type: .system)
button.setTitle(page.title ?? "Page \(index + 1)", for: .normal)
button.titleLabel?.font = .preferredFont(forTextStyle: .subheadline)
button.titleLabel?.adjustsFontForContentSizeCategory = true
button.addAction(UIAction { [weak self] _ in self?.select(index, animated: true) }, for: .touchUpInside)
buttons.append(button)
tabStack.addArrangedSubview(button)
}
tabScroll.showsHorizontalScrollIndicator = false
tabScroll.addSubview(tabStack)
tabScroll.addSubview(indicator)
indicator.backgroundColor = .label
// Pages.
addChild(pageController)
pageController.dataSource = self
pageController.delegate = self
pageController.setViewControllers([pages[selectedIndex]], direction: .forward, animated: false)
for v in [tabScroll, pageController.view!, tabStack] { v.translatesAutoresizingMaskIntoConstraints = false }
view.addSubview(tabScroll)
view.addSubview(pageController.view)
NSLayoutConstraint.activate([
tabScroll.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tabScroll.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tabScroll.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tabScroll.heightAnchor.constraint(equalToConstant: 44),
tabStack.topAnchor.constraint(equalTo: tabScroll.contentLayoutGuide.topAnchor),
tabStack.bottomAnchor.constraint(equalTo: tabScroll.contentLayoutGuide.bottomAnchor),
tabStack.leadingAnchor.constraint(equalTo: tabScroll.contentLayoutGuide.leadingAnchor, constant: 16),
tabStack.trailingAnchor.constraint(equalTo: tabScroll.contentLayoutGuide.trailingAnchor, constant: -16),
tabStack.heightAnchor.constraint(equalTo: tabScroll.frameLayoutGuide.heightAnchor),
pageController.view.topAnchor.constraint(equalTo: tabScroll.bottomAnchor),
pageController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
pageController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
pageController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
pageController.didMove(toParent: self)
updateTabs(animated: false)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateTabs(animated: false)
}
/// Shows page `index`, from a tab tap or from code.
func select(_ index: Int, animated: Bool) {
guard index != selectedIndex, pages.indices.contains(index) else { return }
let direction: UIPageViewController.NavigationDirection = index > selectedIndex ? .forward : .reverse
selectedIndex = index
pageController.setViewControllers([pages[index]], direction: direction, animated: animated)
updateTabs(animated: animated)
}
private func updateTabs(animated: Bool) {
guard isViewLoaded, buttons.indices.contains(selectedIndex) else { return }
for (index, button) in buttons.enumerated() {
button.tintColor = index == selectedIndex ? .label : .secondaryLabel
button.accessibilityTraits = index == selectedIndex ? [.button, .selected] : .button
}
tabScroll.layoutIfNeeded() // the buttons' frames must be current before we measure them
let selected = buttons[selectedIndex]
let frame = selected.convert(selected.bounds, to: tabScroll)
let move = {
self.indicator.frame = CGRect(x: frame.minX, y: self.tabScroll.bounds.height - 2, width: frame.width, height: 2)
self.tabScroll.scrollRectToVisible(frame.insetBy(dx: -40, dy: 0), animated: false)
}
if animated, !UIAccessibility.isReduceMotionEnabled {
UIView.animate(withDuration: 0.25, animations: move)
} else {
move()
}
}
// MARK: Swiping between pages
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let index = pages.firstIndex(of: viewController), index > 0 else { return nil }
return pages[index - 1]
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let index = pages.firstIndex(of: viewController), index < pages.count - 1 else { return nil }
return pages[index + 1]
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool,
previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
guard completed, let current = pageViewController.viewControllers?.first,
let index = pages.firstIndex(of: current) else { return }
selectedIndex = index
updateTabs(animated: true)
}
}
What the test showed
I made six pages titled Item1 to Item6, created the controller with initialIndex: 2, laid it out at 320 points wide, and then selected tabs from code:
Right after pushViewController (no window): view loaded = false
After layout: view loaded = true, index seen in viewDidLoad = 2
Start (initialIndex 2): selectedIndex 2, page shown Item3, bar under tab 2, tabs scrolled to x 0 of 375 wide
After select(5) : selectedIndex 5, page shown Item6, bar under tab 5, tabs scrolled to x 55 of 375 wide
After select(0) : selectedIndex 0, page shown Item1, bar under tab 0, tabs scrolled to x 0 of 375 wide
Swipe past the first page: nothing; past the last: nothing
- It opened on the third tab, Item3, with the bar under tab 2 (counting from 0), with no scrolling from the first tab.
- Selecting the last tab showed Item6, moved the bar under it, and scrolled the tab row 55 points so the tab was visible; the six titles needed 375 points in a 320-point row.
- Swiping stops at the ends. The data source returns nothing before the first page or after the last.
- The first two lines are the 2018 push order with a stand-in screen, described above.
How it works
- The pages are a
UIPageViewControllerwith the scroll transition, so swiping and the system's page behavior come for free.didFinishAnimatingtells the controller which page a swipe ended on, and the tabs follow. - The tabs are buttons in a scroll view, so six or sixteen titles fit on any screen width.
scrollRectToVisiblekeeps the selected tab in view, with some room on each side. - The bar is measured after layout.
tabScroll.layoutIfNeeded()runs before the selected button's frame is read. My first version skipped it and the bar had no position on the first layout, because the buttons hadn't been placed yet when the controller's own layout ran. - Accessibility and text size. The selected tab gets the
.selectedtrait, so VoiceOver says which tab is current, and the titles use a text style that follows the user's text size. The bar moves without animation when Reduce Motion is on. - Colors adapt.
.labeland.secondaryLabelfor the tabs and the bar work in light and dark mode, where the 2018 appearance used fixed black and a green navigation bar.
A quick check for your project
- Search for properties set on a view controller right after
pushViewControllerorpresent. Move those values into the initializer. - If you use Tabman or Pageboy, check what
defaultPage(for:)returns, and use it instead of scrolling inviewDidLoad. - Try the tabs at the largest text size and on the narrowest device you support, and check that the selected tab stays visible.
What the 2018 version said
The post showed both a short example with two pages and the full six-page screen, configured with bar.style = .scrollingButtonBar and an appearance block: a black indicator with normal weight that compresses, a 12-point medium font, a 5-point bar height and 15 points between items. It started with a header image loaded from Tabman's GitHub repository, which no longer exists at that address, so I left it out.
Keep reading on JR Devhub
Comments