1

我是 iOS 开发的新手,我正在尝试通过在页面之间滑动来重新创建 Android 样式的顶部选项卡。https://material.io/design/components/tabs.html#behavior

我正在使用 UIPageViewController 和 MDCTabBar 来复制 Android ViewPager+TabView 组合。但是,我不能使标签指示器(活动标签的突出显示)与标签滑动一起移动。使用 MDCTabBar 是不可能的还是我错过了什么?

4

1 回答 1

1

假设您已UIPageViewControllerDelegate在视图控制器中实现,您可以执行以下操作:

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

MDCTabBarDelegate部分:

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)
}
于 2018-10-11T15:55:38.980 回答