1

I am new to iOS development and I am trying to recreate Android style top tabs with swiping between pages. https://material.io/design/components/tabs.html#behavior

I am using the UIPageViewController together with MDCTabBar to replicate Android ViewPager+TabView combo. However, I cannot make the tab indicator (an highlight for active tab) move along with tabs swipe. Is it impossible using the MDCTabBar or am I missing something?

EvilDuck
  • 4,386
  • 23
  • 32

1 Answers1

1

Assuming that you've implemented UIPageViewControllerDelegate in the view controller you can do the following:

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
    let index = pages.index(of: pendingViewControllers[0])!
    tabBarView.setSelectedItem(tabBarView.items[index], animated: true)
}

Plus MDCTabBarDelegate part:

func tabBar(_ tabBar: MDCTabBar, willSelect item: UITabBarItem) {
    let currentIndex = tabBarView.items.index(of: tabBarView.selectedItem!)!
    let nextIndex = tabBarView.items.index(of: item)!
    var direction = currentIndex < nextIndex ? UIPageViewControllerNavigationDirection.forward :  UIPageViewControllerNavigationDirection.reverse

    setViewControllers([pages[nextIndex]], direction: direction, animated: true, completion: nil)
}
kmityak
  • 461
  • 5
  • 11