Skip to content

Commit

Permalink
change tabItems type to [UIViewController]
Browse files Browse the repository at this point in the history
  • Loading branch information
simorgh3196 committed Aug 5, 2017
1 parent 959e589 commit cbe08a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
11 changes: 9 additions & 2 deletions Demo/TabPageViewControllerDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class ViewController: UIViewController {
let tc = TabPageViewController.create()
let vc1 = UIViewController()
vc1.view.backgroundColor = UIColor.white
vc1.title = "First"
let vc2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ListViewController")
tc.tabItems = [(vc1, "First"), (vc2, "Second")]
vc2.title = "Second"
tc.tabItems = [vc1, vc2]
var option = TabPageOption()
option.tabWidth = view.frame.width / CGFloat(tc.tabItems.count)
option.hidesTopViewOnSwipeType = .all
Expand All @@ -38,15 +40,20 @@ class ViewController: UIViewController {
let tc = TabPageViewController.create()
let vc1 = UIViewController()
vc1.view.backgroundColor = UIColor(red: 251/255, green: 252/255, blue: 149/255, alpha: 1.0)
vc1.title = "Mon."
let vc2 = UIViewController()
vc2.view.backgroundColor = UIColor(red: 252/255, green: 150/255, blue: 149/255, alpha: 1.0)
vc2.title = "Tue."
let vc3 = UIViewController()
vc3.view.backgroundColor = UIColor(red: 149/255, green: 218/255, blue: 252/255, alpha: 1.0)
vc3.title = "Wed."
let vc4 = UIViewController()
vc4.view.backgroundColor = UIColor(red: 149/255, green: 252/255, blue: 197/255, alpha: 1.0)
vc4.title = "Thu."
let vc5 = UIViewController()
vc5.view.backgroundColor = UIColor(red: 252/255, green: 182/255, blue: 106/255, alpha: 1.0)
tc.tabItems = [(vc1, "Mon."), (vc2, "Tue."), (vc3, "Wed."), (vc4, "Thu."), (vc5, "Fri.")]
vc5.title = "Fri."
tc.tabItems = [vc1, vc2, vc3, vc4, vc5]
tc.isInfinity = true
let nc = UINavigationController()
nc.viewControllers = [tc]
Expand Down
15 changes: 7 additions & 8 deletions Sources/TabPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import UIKit
open class TabPageViewController: UIPageViewController {
open var isInfinity: Bool = false
open var option: TabPageOption = TabPageOption()
open var tabItems: [(viewController: UIViewController, title: String)] = []
open var tabItems: [UIViewController] = []

var currentIndex: Int? {
guard let viewController = viewControllers?.first else {
return nil
}
return tabItems.map{ $0.viewController }.index(of: viewController)
return tabItems.index(of: viewController)
}
fileprivate var beforeIndex: Int = 0
fileprivate var tabItemsCount: Int {
Expand Down Expand Up @@ -81,7 +81,7 @@ public extension TabPageViewController {

beforeIndex = index
shouldScrollCurrentBar = false
let nextViewControllers: [UIViewController] = [tabItems[index].viewController]
let nextViewControllers = [tabItems[index]]

let completion: ((Bool) -> Void) = { [weak self] _ in
self?.shouldScrollCurrentBar = true
Expand Down Expand Up @@ -109,7 +109,7 @@ extension TabPageViewController {
delegate = self
automaticallyAdjustsScrollViewInsets = false

setViewControllers([tabItems[beforeIndex].viewController],
setViewControllers([tabItems[beforeIndex]],
direction: .forward,
animated: false,
completion: nil)
Expand Down Expand Up @@ -175,7 +175,7 @@ extension TabPageViewController {

view.addConstraints([top, left, right])

tabView.pageTabItems = tabItems.map({ $0.title})
tabView.pageTabItems = tabItems.map { $0.title ?? "" }
tabView.updateCurrentIndex(beforeIndex, shouldScroll: true)

tabView.pageItemPressedBlock = { [weak self] (index: Int, direction: UIPageViewControllerNavigationDirection) in
Expand Down Expand Up @@ -292,8 +292,7 @@ extension TabPageViewController {
extension TabPageViewController: UIPageViewControllerDataSource {

fileprivate func nextViewController(_ viewController: UIViewController, isAfter: Bool) -> UIViewController? {

guard var index = tabItems.map({$0.viewController}).index(of: viewController) else {
guard var index = tabItems.index(of: viewController) else {
return nil
}

Expand All @@ -312,7 +311,7 @@ extension TabPageViewController: UIPageViewControllerDataSource {
}

if index >= 0 && index < tabItems.count {
return tabItems[index].viewController
return tabItems[index]
}
return nil
}
Expand Down

0 comments on commit cbe08a2

Please sign in to comment.