diff --git a/Demo/TabPageViewControllerDemo/ViewController.swift b/Demo/TabPageViewControllerDemo/ViewController.swift index f270845..6f33a89 100644 --- a/Demo/TabPageViewControllerDemo/ViewController.swift +++ b/Demo/TabPageViewControllerDemo/ViewController.swift @@ -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 @@ -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] diff --git a/README.md b/README.md index 9fbaeaa..5b9c2b5 100644 --- a/README.md +++ b/README.md @@ -86,9 +86,11 @@ Use TabPageOption ```swift let tabPageViewController = TabPageViewController.create() let vc1 = UIViewController() +vc1.title = "First" let vc2 = UIViewController() +vc2.title = "Second" -tabPageViewController.tabItems = [(vc1, "First"), (vc2, "Second")] +tabPageViewController.tabItems = [vc1, vc2] TabPageOption.currentColor = UIColor.redColor() diff --git a/Sources/TabPageViewController.swift b/Sources/TabPageViewController.swift index 2bcabc1..ce5fa7a 100644 --- a/Sources/TabPageViewController.swift +++ b/Sources/TabPageViewController.swift @@ -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 { @@ -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 @@ -109,7 +109,7 @@ extension TabPageViewController { delegate = self automaticallyAdjustsScrollViewInsets = false - setViewControllers([tabItems[beforeIndex].viewController], + setViewControllers([tabItems[beforeIndex]], direction: .forward, animated: false, completion: nil) @@ -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 @@ -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 } @@ -312,7 +311,7 @@ extension TabPageViewController: UIPageViewControllerDataSource { } if index >= 0 && index < tabItems.count { - return tabItems[index].viewController + return tabItems[index] } return nil }