// // SegmentedPageViewController.swift // Tusker // // Created by Shadowfacts on 9/13/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import UIKit class SegmentedPageViewController: UIPageViewController, UIPageViewControllerDelegate { let titles: [String] let pageControllers: [UIViewController] private(set) var currentIndex: Int! var segmentedControl: UISegmentedControl! init(titles: [String], pageControllers: [UIViewController]) { self.titles = titles self.pageControllers = pageControllers super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil) self.delegate = self } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .systemBackground segmentedControl = UISegmentedControl(items: titles) segmentedControl.addTarget(self, action: #selector(segmentedControlChanged), for: .valueChanged) navigationItem.titleView = segmentedControl segmentedControl.selectedSegmentIndex = 0 selectPage(at: 0, animated: false) addKeyCommand(MenuController.prevSubTabCommand) addKeyCommand(MenuController.nextSubTabCommand) // disable the transparent nav bar because it gets messy with multiple pages at different scroll positions if let nav = navigationController { let appearance = UINavigationBarAppearance() appearance.configureWithDefaultBackground() nav.navigationBar.scrollEdgeAppearance = appearance } } func selectPage(at index: Int, animated: Bool) { let direction: UIPageViewController.NavigationDirection = currentIndex == nil ? .forward : index - currentIndex > 0 ? .forward : .reverse setViewControllers([pageControllers[index]], direction: direction, animated: animated) navigationItem.title = pageControllers[index].title currentIndex = index segmentedControl.selectedSegmentIndex = index } @objc func segmentedControlChanged() { selectPage(at: segmentedControl.selectedSegmentIndex, animated: true) UIImpactFeedbackGenerator(style: .light).impactOccurred() } // MARK: - Page View Controller Delegate func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { currentIndex = pageControllers.firstIndex(of: viewControllers!.first!)! segmentedControl.selectedSegmentIndex = currentIndex navigationItem.title = viewControllers!.first!.title } } extension SegmentedPageViewController: TabBarScrollableViewController { func tabBarScrollToTop() { if let scrollableVC = pageControllers[currentIndex] as? TabBarScrollableViewController { scrollableVC.tabBarScrollToTop() } } } extension SegmentedPageViewController: TabbedPageViewController { func selectNextPage() { guard currentIndex < pageControllers.count - 1 else { return } selectPage(at: currentIndex + 1, animated: true) } func selectPrevPage() { guard currentIndex > 0 else { return } selectPage(at: currentIndex - 1, animated: true) } } extension SegmentedPageViewController: BackgroundableViewController { func sceneDidEnterBackground() { if let current = pageControllers[currentIndex] as? BackgroundableViewController { current.sceneDidEnterBackground() } } }