UIPageViewController return the current visible view
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIPageViewController is a solid choice for swipe-based onboarding, galleries, and step flows, but many bugs come from incorrect current-page tracking. The visible view controller can change during gestures, canceled swipes, and programmatic navigation. A correct implementation treats transition completion as the single source of truth.
Build a Stable Paging Model
Start with a persistent array of page controllers. Do not recreate controllers every time the data source asks for before or after pages. Identity stability makes index lookup reliable.
This setup guarantees that page identity and order remain deterministic.
Read the Currently Visible Page Safely
When the page view controller is idle, the current visible controller is viewControllers?.first. This is useful for immediate checks, but avoid calling it before initial setup.
If you need the current index, resolve it from the same pages array:
Keep this as a read helper. Do not mutate state from it during active transition callbacks.
Update State Only When a Transition Finishes
The most important callback is didFinishAnimating. Update index only when transitionCompleted is true. That avoids incorrect state when a swipe starts but snaps back.
This one rule prevents most page indicator and analytics drift.
Keep Data Source Logic Symmetric
Your data source methods must use the same array and indexing rules as your tracking logic. If one method uses a filtered list or a recreated controller, the visible index can become invalid.
If you later make pages dynamic, update all related logic together.
Programmatic Navigation Without State Drift
Many apps have a next button in addition to swipe gestures. Programmatic navigation should update currentIndex from completion, not from the requested target alone.
This keeps gesture-driven and button-driven behavior consistent.
Connect Page Tracking to UI Elements
If you use UIPageControl, update it in one place, typically after successful transition completion. The same applies to analytics and event logging. One update path avoids duplicate events and contradictory state.
A practical pattern is to create a small onPageChanged(index:) method and call it from both gesture completion and programmatic completion.
Common Pitfalls
- Recreating page controllers in data source methods, which breaks identity checks and index lookup.
- Updating index during gesture start instead of completion, causing wrong state after canceled swipes.
- Reading
viewControllers?.firstbefore initialsetViewControllerscall, which returns no useful value. - Maintaining separate order arrays for data source and UI logic, leading to mismatch.
- Setting
currentIndeximmediately on button tap without waiting for programmatic transition completion.
Summary
- Keep a stable
pagesarray and reuse those controller instances. - Treat transition completion as the authoritative moment for index changes.
- Resolve visible page index from controller identity, not assumptions.
- Use shared logic for swipe and button navigation state updates.
- Centralize UI and analytics page-change handling to avoid duplicate or stale events.
Related reading
- UIPanGestureRecognizer - Only vertical or horizontal
- UIPopovercontroller dealloc reached while popover is still visible
- UIRefreshControl - beginRefreshing not working when UITableViewController is inside UINavigationController
- UIRefreshControl on UICollectionView only works if the collection fills the height of the container
- UIRefreshControl without UITableViewController
- UIScrollView horizontal paging like Mobile Safari tabs
- UIScrollView not scrolling
- UIScrollView paging horizontally, scrolling vertically?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.