我是iOS开发的新手,我试图通过在页面之间滑动来重新创建Android样式的顶部标签。 https://material.io/design/components/tabs.html#behavior
我将UIPageViewController与MDCTabBar一起使用来复制Android ViewPager + TabView组合。但是,我无法使选项卡指示器(活动选项卡的突出显示)与选项卡滑动一起移动。使用MDCTabBar不可能吗,还是我错过了什么?
答案 0 :(得分: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)
}