Skip to content

Commit cbe08a2

Browse files
committed
change tabItems type to [UIViewController]
1 parent 959e589 commit cbe08a2

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

Demo/TabPageViewControllerDemo/ViewController.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ class ViewController: UIViewController {
2525
let tc = TabPageViewController.create()
2626
let vc1 = UIViewController()
2727
vc1.view.backgroundColor = UIColor.white
28+
vc1.title = "First"
2829
let vc2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ListViewController")
29-
tc.tabItems = [(vc1, "First"), (vc2, "Second")]
30+
vc2.title = "Second"
31+
tc.tabItems = [vc1, vc2]
3032
var option = TabPageOption()
3133
option.tabWidth = view.frame.width / CGFloat(tc.tabItems.count)
3234
option.hidesTopViewOnSwipeType = .all
@@ -38,15 +40,20 @@ class ViewController: UIViewController {
3840
let tc = TabPageViewController.create()
3941
let vc1 = UIViewController()
4042
vc1.view.backgroundColor = UIColor(red: 251/255, green: 252/255, blue: 149/255, alpha: 1.0)
43+
vc1.title = "Mon."
4144
let vc2 = UIViewController()
4245
vc2.view.backgroundColor = UIColor(red: 252/255, green: 150/255, blue: 149/255, alpha: 1.0)
46+
vc2.title = "Tue."
4347
let vc3 = UIViewController()
4448
vc3.view.backgroundColor = UIColor(red: 149/255, green: 218/255, blue: 252/255, alpha: 1.0)
49+
vc3.title = "Wed."
4550
let vc4 = UIViewController()
4651
vc4.view.backgroundColor = UIColor(red: 149/255, green: 252/255, blue: 197/255, alpha: 1.0)
52+
vc4.title = "Thu."
4753
let vc5 = UIViewController()
4854
vc5.view.backgroundColor = UIColor(red: 252/255, green: 182/255, blue: 106/255, alpha: 1.0)
49-
tc.tabItems = [(vc1, "Mon."), (vc2, "Tue."), (vc3, "Wed."), (vc4, "Thu."), (vc5, "Fri.")]
55+
vc5.title = "Fri."
56+
tc.tabItems = [vc1, vc2, vc3, vc4, vc5]
5057
tc.isInfinity = true
5158
let nc = UINavigationController()
5259
nc.viewControllers = [tc]

Sources/TabPageViewController.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import UIKit
1111
open class TabPageViewController: UIPageViewController {
1212
open var isInfinity: Bool = false
1313
open var option: TabPageOption = TabPageOption()
14-
open var tabItems: [(viewController: UIViewController, title: String)] = []
14+
open var tabItems: [UIViewController] = []
1515

1616
var currentIndex: Int? {
1717
guard let viewController = viewControllers?.first else {
1818
return nil
1919
}
20-
return tabItems.map{ $0.viewController }.index(of: viewController)
20+
return tabItems.index(of: viewController)
2121
}
2222
fileprivate var beforeIndex: Int = 0
2323
fileprivate var tabItemsCount: Int {
@@ -81,7 +81,7 @@ public extension TabPageViewController {
8181

8282
beforeIndex = index
8383
shouldScrollCurrentBar = false
84-
let nextViewControllers: [UIViewController] = [tabItems[index].viewController]
84+
let nextViewControllers = [tabItems[index]]
8585

8686
let completion: ((Bool) -> Void) = { [weak self] _ in
8787
self?.shouldScrollCurrentBar = true
@@ -109,7 +109,7 @@ extension TabPageViewController {
109109
delegate = self
110110
automaticallyAdjustsScrollViewInsets = false
111111

112-
setViewControllers([tabItems[beforeIndex].viewController],
112+
setViewControllers([tabItems[beforeIndex]],
113113
direction: .forward,
114114
animated: false,
115115
completion: nil)
@@ -175,7 +175,7 @@ extension TabPageViewController {
175175

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

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

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

294294
fileprivate func nextViewController(_ viewController: UIViewController, isAfter: Bool) -> UIViewController? {
295-
296-
guard var index = tabItems.map({$0.viewController}).index(of: viewController) else {
295+
guard var index = tabItems.index(of: viewController) else {
297296
return nil
298297
}
299298

@@ -312,7 +311,7 @@ extension TabPageViewController: UIPageViewControllerDataSource {
312311
}
313312

314313
if index >= 0 && index < tabItems.count {
315-
return tabItems[index].viewController
314+
return tabItems[index]
316315
}
317316
return nil
318317
}

0 commit comments

Comments
 (0)