2 // PageViewController.swift
5 // Created by Heberti Almeida on 14/07/16.
6 // Copyright © 2016 FolioReader. All rights reserved.
11 class PageViewController: UIPageViewController {
13 var segmentedControl: UISegmentedControl!
14 var viewList = [UIViewController]()
15 var segmentedControlItems = [String]()
16 var viewControllerOne: UIViewController!
17 var viewControllerTwo: UIViewController!
19 fileprivate var readerConfig: FolioReaderConfig
20 fileprivate var folioReader: FolioReader
24 init(folioReader: FolioReader, readerConfig: FolioReaderConfig) {
25 self.folioReader = folioReader
26 self.readerConfig = readerConfig
27 self.index = self.folioReader.currentMenuIndex
28 super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
30 self.edgesForExtendedLayout = UIRectEdge()
31 self.extendedLayoutIncludesOpaqueBars = true
34 required init?(coder: NSCoder) {
35 fatalError("storyboards are incompatible with truth and beauty")
38 override func viewDidLoad() {
41 segmentedControl = UISegmentedControl(items: segmentedControlItems)
42 segmentedControl.addTarget(self, action: #selector(PageViewController.didSwitchMenu(_:)), for: UIControlEvents.valueChanged)
43 segmentedControl.selectedSegmentIndex = index
44 segmentedControl.setWidth(100, forSegmentAt: 0)
45 segmentedControl.setWidth(100, forSegmentAt: 1)
46 self.navigationItem.titleView = segmentedControl
48 viewList = [viewControllerOne, viewControllerTwo]
50 viewControllerOne.didMove(toParentViewController: self)
51 viewControllerTwo.didMove(toParentViewController: self)
54 self.dataSource = self
56 self.view.backgroundColor = UIColor.white
57 self.setViewControllers([viewList[index]], direction: .forward, animated: false, completion: nil)
59 // FIXME: This disable scroll because of highlight swipe to delete, if you can fix this would be awesome
60 for view in self.view.subviews {
61 if view is UIScrollView {
62 let scroll = view as! UIScrollView
63 scroll.bounces = false
68 let greenColor = UIColor(red:0.00, green:0.51, blue:0.53, alpha:1.00)
69 let tintColor = folioReader.isNight(greenColor, UIColor.white)
71 let closeImage = UIImage(readerImageNamed: "icon-navbar-close")?.imageTintColor(tintColor)?.withRenderingMode(.alwaysOriginal)// ignoreSystemTint(withConfiguration: readerConfig)
72 self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: closeImage, style: .plain, target: self, action: #selector(dismiss as () -> Void))
75 override func viewWillAppear(_ animated: Bool) {
76 super.viewWillAppear(animated)
81 func configureNavBar() {
83 let greenColor = UIColor(red:0.00, green:0.51, blue:0.53, alpha:1.00)
84 let navBackground = folioReader.isNight(self.readerConfig.nightModeMenuBackground, greenColor)
85 let tintColor = folioReader.isNight(greenColor, UIColor.white)
86 let navText = tintColor
87 let font = UIFont(name: "Avenir-Light", size: 17)!
88 setTranslucentNavigation(color: navBackground, tintColor: tintColor, titleColor: navText, andFont: font)
91 // MARK: - Segmented control changes
93 @objc func didSwitchMenu(_ sender: UISegmentedControl) {
94 self.index = sender.selectedSegmentIndex
95 let direction: UIPageViewControllerNavigationDirection = (index == 0 ? .reverse : .forward)
96 setViewControllers([viewList[index]], direction: direction, animated: true, completion: nil)
97 self.folioReader.currentMenuIndex = index
100 // MARK: - Status Bar
103 override var preferredStatusBarStyle : UIStatusBarStyle {
104 return .lightContent //self.folioReader.isNight(.lightContent, .default)
108 // MARK: UIPageViewControllerDelegate
110 extension PageViewController: UIPageViewControllerDelegate {
112 func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
114 if finished && completed {
115 let viewController = pageViewController.viewControllers?.last
116 segmentedControl.selectedSegmentIndex = viewList.index(of: viewController!)!
121 // MARK: UIPageViewControllerDataSource
123 extension PageViewController: UIPageViewControllerDataSource {
125 func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
127 let index = viewList.index(of: viewController)!
128 if index == viewList.count - 1 {
132 self.index = self.index + 1
133 return viewList[self.index]
136 func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
138 let index = viewList.index(of: viewController)!
143 self.index = self.index - 1
144 return viewList[self.index]