forked from eRGoon/RGPageViewController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RGPageViewController.swift
958 lines (757 loc) · 44.2 KB
/
RGPageViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
//
// RGPageViewController.swift
// RGViewPager
//
// Created by Ronny Gerasch on 08.11.14.
// Copyright (c) 2014 Ronny Gerasch. All rights reserved.
//
import Foundation
import UIKit
enum RGTabbarStyle {
case Solid
case Blurred
}
enum RGTabStyle {
case None
case InactiveFaded
}
enum RGTabbarPosition {
case Top
case Bottom
case Left
case Right
}
class RGPageViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate, UIToolbarDelegate {
// protocols
weak var datasource: RGPageViewControllerDataSource?
weak var delegate: RGPageViewControllerDelegate?
var pageViewScrollDelegate: UIScrollViewDelegate?
// variables
var animatingToTab: Bool = false
var needsSetup: Bool = true
var needsLayoutSubviews = true
// pager
var pageViewControllers: NSMutableArray = NSMutableArray()
var pageCount: Int! = 0
var currentPageIndex: Int = 0
var pager: UIPageViewController!
var pagerOrientation: UIPageViewControllerNavigationOrientation {
get {
return .Horizontal
}
}
var pagerView: UIView?
var pagerScrollView: UIScrollView?
// tabs
var tabs: NSMutableArray = NSMutableArray()
var currentTabIndex: Int = 0
var tabWidth: CGFloat = UIScreen.mainScreen().bounds.size.width / 3.0
var tabbarWidth: CGFloat = 100.0
var tabbarHeight: CGFloat = 38.0
var tabIndicatorWidthOrHeight: CGFloat = 2.0
var tabIndicatorColor: UIColor = UIColor.lightGrayColor()
var tabMargin: CGFloat = 0.0
var tabStyle: RGTabStyle {
get {
return .None
}
}
// tabbar
var tabbarStyle: RGTabbarStyle {
get {
return .Blurred
}
}
var tabbarPosition: RGTabbarPosition {
get {
return .Top
}
}
var tabbar: UIView!
var barTintColor: UIColor?
var tabScrollView: UIScrollView = UIScrollView()
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initSelf()
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.initSelf()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if self.needsSetup {
self.setupSelf()
}
}
override func viewWillLayoutSubviews() {
if self.needsLayoutSubviews {
self.layoutSubviews()
self.selectTabAtIndex(self.currentTabIndex)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
private func layoutSubviews() {
var constraints: NSMutableArray!
switch self.tabbarPosition {
case .Top:
constraints = layoutTabbarTop()
case .Bottom:
constraints = layoutTabbarBottom()
case .Left:
constraints = layoutTabbarLeft()
case .Right:
constraints = layoutTabbarRight()
}
self.view.addConstraints(constraints)
self.needsLayoutSubviews = false
}
private func layoutTabbarTop() -> NSMutableArray {
let constraints: NSMutableArray = NSMutableArray()
// remove hairline image in navigation bar if attached to top
if let navController = self.navigationController {
navController.navigationBar.hideHairline()
}
// tabbar constraints
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.topLayoutGuide, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: self.tabbarHeight))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
// tabbar scrollView constraints
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
// tabpager constraints
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
return constraints
}
private func layoutTabbarBottom() -> NSMutableArray {
let constraints: NSMutableArray = NSMutableArray()
// tabbar constraints
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.bottomLayoutGuide, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: self.tabbarHeight))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
// tabbar scrollView constraints
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
// tabpager constraints
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
return constraints
}
private func layoutTabbarLeft() -> NSMutableArray {
let constraints: NSMutableArray = NSMutableArray()
// scroll tabbar under topbar if using solid style
if self.tabbarStyle == RGTabbarStyle.Solid {
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
var edgeInsets: UIEdgeInsets = self.tabScrollView.contentInset
edgeInsets.top = self.topLayoutGuide.length
edgeInsets.bottom = self.bottomLayoutGuide.length
self.tabScrollView.contentInset = edgeInsets
self.tabScrollView.scrollIndicatorInsets = edgeInsets
} else {
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.topLayoutGuide, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.bottomLayoutGuide, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
}
// tabbar constraints
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: self.tabbarWidth))
// tabbar scrollView constraints
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
// tabpager constraints
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
return constraints
}
private func layoutTabbarRight() -> NSMutableArray {
let constraints: NSMutableArray = NSMutableArray()
// scroll tabbar under topbar if using solid style
if self.tabbarStyle == RGTabbarStyle.Solid {
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
var edgeInsets: UIEdgeInsets = self.tabScrollView.contentInset
edgeInsets.top = self.topLayoutGuide.length
edgeInsets.bottom = self.bottomLayoutGuide.length
self.tabScrollView.contentInset = edgeInsets
self.tabScrollView.scrollIndicatorInsets = edgeInsets
} else {
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.topLayoutGuide, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
}
// tabbar constraints
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: self.tabbarWidth))
constraints.addObject(NSLayoutConstraint(item: self.tabbar, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.bottomLayoutGuide, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
// tabbar scrollView constraints
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.tabScrollView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
// tabpager constraints
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.tabbar, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
constraints.addObject(NSLayoutConstraint(item: self.pagerView!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
return constraints
}
private func initSelf() {
// init pager
self.pager = UIPageViewController(transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: self.pagerOrientation, options: nil)
// init tabbar
switch self.tabbarStyle {
case .Blurred:
self.tabbar = UIToolbar(frame: CGRectZero)
case .Solid:
self.tabbar = UIView(frame: CGRectZero)
}
self.addChildViewController(self.pager)
self.pagerView = self.pager.view
self.pagerScrollView = self.pagerView!.subviews[0] as? UIScrollView
self.pageViewScrollDelegate = self.pagerScrollView?.delegate
self.pagerScrollView!.scrollsToTop = false
self.pagerScrollView!.delegate = self
self.pager.dataSource = self
self.pager.delegate = self
self.animatingToTab = false
self.needsSetup = true
}
private func setupSelf() {
for tabView in self.tabs {
(tabView as RGTabView).removeFromSuperview()
}
self.tabScrollView.contentSize = CGSizeZero
self.tabs.removeAllObjects()
self.pageViewControllers.removeAllObjects()
if let theSource = self.datasource {
self.pageCount = theSource.numberOfPagesForViewController(self)
}
self.tabs = NSMutableArray(capacity: self.pageCount)
self.pageViewControllers = NSMutableArray(capacity: self.pageCount)
for i in 0 ..< self.pageCount {
self.tabs.addObject(NSNull())
self.pageViewControllers.addObject(NSNull())
}
// init Tabbar
if let theDelegate = self.delegate {
if let tabHeight = theDelegate.heightForTabbar?() {
self.tabbarHeight = tabHeight
}
if let tabWidth = theDelegate.widthForTabbar?() {
self.tabbarWidth = tabWidth
}
if let indicatorColor = theDelegate.colorForTabIndicator?() {
self.tabIndicatorColor = indicatorColor
}
if let indicatorHW = theDelegate.widthOrHeightForIndicator?() {
self.tabIndicatorWidthOrHeight = indicatorHW
}
if let tintColor = theDelegate.tintColorForTabBar?() {
self.barTintColor = tintColor
switch self.tabbarStyle {
case .Blurred:
(self.tabbar as UIToolbar).barTintColor = self.barTintColor
case .Solid:
self.tabbar.backgroundColor = self.barTintColor
}
}
}
var tabScrollFrame: CGRect = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.tabbarHeight)
switch self.tabbarPosition {
case .Top, .Bottom:
tabScrollFrame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.tabbarHeight)
case .Left, .Right:
tabScrollFrame = CGRectMake(0.0, 0.0, self.tabbarWidth, self.view.bounds.size.height)
}
self.tabScrollView.frame = tabScrollFrame
self.tabScrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.tabScrollView.backgroundColor = UIColor.clearColor()
self.tabScrollView.scrollsToTop = false
self.tabScrollView.opaque = false
self.tabScrollView.showsHorizontalScrollIndicator = false
self.tabScrollView.showsVerticalScrollIndicator = false
var contentSizeWH: CGFloat = self.tabMargin / 2.0
for i in 0 ..< self.pageCount {
if let tabView: RGTabView = self.tabViewAtIndex(i) {
var frame: CGRect = tabView.frame
switch self.tabbarPosition {
case .Top, .Bottom:
frame.origin.x = contentSizeWH
contentSizeWH += CGRectGetWidth(frame) + self.tabMargin
case .Left, .Right:
frame.origin.y = contentSizeWH
contentSizeWH += CGRectGetHeight(frame) + self.tabMargin
}
tabView.frame = frame
self.tabScrollView.addSubview(tabView)
var tapRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
tabView.addGestureRecognizer(tapRecognizer)
}
}
contentSizeWH -= self.tabMargin / 2.0
switch self.tabbarPosition {
case .Top, .Bottom:
self.tabScrollView.contentSize = CGSizeMake(contentSizeWH, self.tabbarHeight)
case .Left, .Right:
self.tabScrollView.contentSize = CGSizeMake(self.tabbarWidth, contentSizeWH)
}
if self.tabbarStyle == .Blurred {
(self.tabbar as UIToolbar).translucent = true
(self.tabbar as UIToolbar).delegate = self
}
self.tabbar.setTranslatesAutoresizingMaskIntoConstraints(false)
self.tabbar.addSubview(self.tabScrollView)
self.pagerView!.setTranslatesAutoresizingMaskIntoConstraints(false)
self.pagerView!.autoresizingMask = (UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth)
self.view.addSubview(self.pagerView!)
self.view.addSubview(self.tabbar)
self.needsSetup = false
}
private func tabViewAtIndex(index: Int) -> RGTabView? {
if index >= self.pageCount {
return nil
}
if self.tabs.objectAtIndex(index).isEqual(NSNull()) {
if let tabViewContent: UIView = self.datasource?.tabViewForPageAtIndex(self, index: index) {
var tabView: RGTabView
switch self.tabbarPosition {
case .Top, .Bottom:
if let theWidth: CGFloat = self.delegate?.widthForTabAtIndex?(index) {
tabView = RGTabView(frame: CGRectMake(0.0, 0.0, theWidth, self.tabbarHeight), indicatorColor: self.tabIndicatorColor, indicatorHW: self.tabIndicatorWidthOrHeight, style: self.tabStyle, orientation: .Horizontal)
} else {
tabView = RGTabView(frame: CGRectMake(0.0, 0.0, self.tabWidth, self.tabbarHeight), indicatorColor: self.tabIndicatorColor, indicatorHW: self.tabIndicatorWidthOrHeight, style: self.tabStyle, orientation: .Horizontal)
}
case .Left:
if let theHeight: CGFloat = self.delegate?.heightForTabAtIndex?(index) {
tabView = RGTabView(frame: CGRectMake(0.0, 0.0, self.tabbarWidth, theHeight), indicatorColor: self.tabIndicatorColor, indicatorHW: self.tabIndicatorWidthOrHeight, style: self.tabStyle, orientation: .VerticalLeft)
} else {
tabView = RGTabView(frame: CGRectMake(0.0, 0.0, self.tabbarWidth, self.tabbarWidth), indicatorColor: self.tabIndicatorColor, indicatorHW: self.tabIndicatorWidthOrHeight, style: self.tabStyle, orientation: .VerticalLeft)
}
case .Right:
if let theHeight: CGFloat = self.delegate?.heightForTabAtIndex?(index) {
tabView = RGTabView(frame: CGRectMake(0.0, 0.0, self.tabbarWidth, theHeight), indicatorColor: self.tabIndicatorColor, indicatorHW: self.tabIndicatorWidthOrHeight, style: self.tabStyle, orientation: .VerticalRight)
} else {
tabView = RGTabView(frame: CGRectMake(0.0, 0.0, self.tabbarWidth, self.tabbarWidth), indicatorColor: self.tabIndicatorColor, indicatorHW: self.tabIndicatorWidthOrHeight, style: self.tabStyle, orientation: .VerticalRight)
}
}
tabView.addSubview(tabViewContent)
tabView.clipsToBounds = true
tabViewContent.center = tabView.center
self.tabs.replaceObjectAtIndex(index, withObject: tabView)
}
}
return self.tabs.objectAtIndex(index) as? RGTabView
}
private func viewControllerAtIndex(index: Int) -> UIViewController? {
if index >= self.pageCount {
return nil
}
if self.pageViewControllers.objectAtIndex(index).isEqual(NSNull()) {
if let vc: UIViewController = self.datasource?.viewControllerForPageAtIndex(self, index: index) {
let view: UIView = vc.view.subviews[0] as UIView
if view is UIScrollView {
let scrollView = (view as UIScrollView)
var edgeInsets: UIEdgeInsets = scrollView.contentInset
if self.tabbarPosition == RGTabbarPosition.Top {
edgeInsets.top = self.topLayoutGuide.length + self.tabbarHeight
} else if self.tabbarPosition == RGTabbarPosition.Bottom {
edgeInsets.top = self.topLayoutGuide.length
edgeInsets.bottom = self.tabbarHeight
} else {
edgeInsets.top = self.topLayoutGuide.length
edgeInsets.bottom = self.bottomLayoutGuide.length
}
scrollView.contentInset = edgeInsets
scrollView.scrollIndicatorInsets = edgeInsets
}
self.pageViewControllers.replaceObjectAtIndex(index, withObject: vc)
}
}
return self.pageViewControllers.objectAtIndex(index) as? UIViewController
}
func handleTap(sender: UITapGestureRecognizer) {
let tabView: RGTabView = sender.view as RGTabView
let i: Int = self.tabs.indexOfObject(tabView)
if self.currentTabIndex != i {
self.selectTabAtIndex(i)
}
}
private func selectTabAtIndex(index: Int) {
if index >= self.pageCount {
return
}
self.animatingToTab = true
self.updateTabIndex(index, animated: true)
self.updatePager(index)
self.delegate?.didChangePageToIndex?(index)
}
private func updateTabIndex(index: Int, animated: Bool) {
let activeTab: RGTabView = self.tabViewAtIndex(self.currentTabIndex)!
let newTab: RGTabView = self.tabViewAtIndex(index)!
activeTab.selected = false
newTab.selected = true
self.currentTabIndex = index
var visibleRect = newTab.frame
switch self.tabbarPosition {
case .Top, .Bottom:
visibleRect.origin.x -= self.tabMargin / 2.0
visibleRect.size.width += self.tabMargin
case .Left, .Right:
visibleRect.origin.y -= self.tabMargin / 2.0
visibleRect.size.height += self.tabMargin
}
self.tabScrollView.scrollRectToVisible(visibleRect, animated: animated)
}
private func updatePager(index: Int) {
if let vc: UIViewController = self.viewControllerAtIndex(index) {
weak var weakSelf: RGPageViewController? = self
weak var weakPager: UIPageViewController? = self.pager
if index == self.currentPageIndex {
self.pager.setViewControllers([vc], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: { (Bool) -> Void in
weakSelf!.animatingToTab = false
})
} else if !(index + 1 == self.currentPageIndex || index - 1 == self.currentPageIndex) {
self.pager.setViewControllers([vc], direction: index < self.currentPageIndex ? UIPageViewControllerNavigationDirection.Reverse : UIPageViewControllerNavigationDirection.Forward, animated: true, completion: { (Bool) -> Void in
weakSelf!.animatingToTab = false
dispatch_async(dispatch_get_main_queue(), {
weakPager!.setViewControllers([vc], direction: index < weakSelf!.currentPageIndex ? UIPageViewControllerNavigationDirection.Reverse : UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
})
})
} else {
self.pager.setViewControllers([vc], direction: index < self.currentPageIndex ? UIPageViewControllerNavigationDirection.Reverse : UIPageViewControllerNavigationDirection.Forward, animated: true, completion: { (Bool) -> Void in
weakSelf!.animatingToTab = false
})
}
self.currentPageIndex = index
}
}
private func indexForViewController(vc: UIViewController) -> (Int) {
return self.pageViewControllers.indexOfObject(vc)
}
// MARK: - Interface rotation
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
self.selectTabAtIndex(self.currentTabIndex)
}
// MARK: - UIToolbarDelegate
func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
var position: UIBarPosition = UIBarPosition.Top
switch self.tabbarPosition {
case .Top:
position = UIBarPosition.Top
case .Bottom:
position = UIBarPosition.Bottom
case .Left, .Right:
position = UIBarPosition.Any
}
return position
}
// MARK: - PageViewController Data Source
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
var index: Int = self.indexForViewController(viewController)
if index == 0 {
return nil
}
index--
return self.viewControllerAtIndex(index)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
var index: Int = self.indexForViewController(viewController)
if index == self.pageCount - 1 {
return nil
}
index++
return self.viewControllerAtIndex(index)
}
// MARK: - PageViewController Delegate
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) {
if (finished && completed) {
let vc: UIViewController = self.pager.viewControllers[0] as UIViewController
let index: Int = self.indexForViewController(vc)
self.selectTabAtIndex(index)
}
}
// MARK: - UIScrollView Delegate
func scrollViewDidScroll(scrollView: UIScrollView) {
self.pageViewScrollDelegate?.scrollViewDidScroll?(scrollView)
}
func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool {
if let shouldScroll = self.pageViewScrollDelegate?.scrollViewShouldScrollToTop?(scrollView) {
return shouldScroll
}
return false
}
func scrollViewDidScrollToTop(scrollView: UIScrollView) {
self.pageViewScrollDelegate?.scrollViewDidScrollToTop?(scrollView)
}
func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
self.pageViewScrollDelegate?.scrollViewDidEndScrollingAnimation?(scrollView)
}
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
self.pageViewScrollDelegate?.scrollViewWillBeginDragging?(scrollView)
}
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
self.pageViewScrollDelegate?.scrollViewWillEndDragging?(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
}
func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
self.pageViewScrollDelegate?.scrollViewDidEndDragging?(scrollView, willDecelerate: decelerate)
}
func scrollViewWillBeginDecelerating(scrollView: UIScrollView) {
self.pageViewScrollDelegate?.scrollViewWillBeginDecelerating?(scrollView)
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
self.pageViewScrollDelegate?.scrollViewDidEndDecelerating?(scrollView)
}
func scrollViewWillBeginZooming(scrollView: UIScrollView, withView view: UIView!) {
self.pageViewScrollDelegate?.scrollViewWillBeginZooming?(scrollView, withView: view)
}
func scrollViewDidZoom(scrollView: UIScrollView) {
self.pageViewScrollDelegate?.scrollViewDidZoom?(scrollView)
}
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView!, atScale scale: CGFloat) {
self.pageViewScrollDelegate?.scrollViewDidEndZooming?(scrollView, withView: view, atScale: scale)
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
if let view: UIView = self.pageViewScrollDelegate?.viewForZoomingInScrollView?(scrollView) {
return view
}
return nil
}
}
// MARK: - RGTabView
class RGTabView: UIView {
enum RGTabOrientation {
case Horizontal
case VerticalLeft
case VerticalRight
}
// variables
var selected: Bool = false {
didSet {
if self.subviews[0] is RGTabBarItem {
(self.subviews[0] as RGTabBarItem).selected = self.selected
} else {
if self.style == RGTabStyle.InactiveFaded {
if self.selected {
self.alpha = 1.0
} else {
self.alpha = 0.566
}
}
self.setNeedsDisplay()
}
}
}
var indicatorHW: CGFloat = 2.0
var indicatorColor: UIColor = UIColor.lightGrayColor()
var orientation: RGTabOrientation = .Horizontal
var style: RGTabStyle = .None
init(frame: CGRect, indicatorColor: UIColor, indicatorHW: CGFloat, style: RGTabStyle, orientation: RGTabOrientation) {
super.init(frame: frame)
self.indicatorColor = indicatorColor
self.orientation = orientation
self.indicatorHW = indicatorHW
self.style = style
self.initSelf()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initSelf()
}
func initSelf() {
self.backgroundColor = UIColor.clearColor()
if self.style == RGTabStyle.InactiveFaded {
self.alpha = 0.566
}
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
if self.selected {
if !(self.subviews[0] is RGTabBarItem) {
var bezierPath: UIBezierPath = UIBezierPath()
if self.orientation == .Horizontal {
bezierPath.moveToPoint(CGPointMake(0.0, CGRectGetHeight(rect) - self.indicatorHW / 2.0))
bezierPath.addLineToPoint(CGPointMake(CGRectGetWidth(rect), CGRectGetHeight(rect) - self.indicatorHW / 2.0))
bezierPath.lineWidth = self.indicatorHW
} else if self.orientation == .VerticalLeft {
bezierPath.moveToPoint(CGPointMake(self.indicatorHW / 2.0, 0.0))
bezierPath.addLineToPoint(CGPointMake(self.indicatorHW / 2.0, CGRectGetHeight(rect)))
bezierPath.lineWidth = self.indicatorHW
} else if self.orientation == .VerticalRight {
bezierPath.moveToPoint(CGPointMake(CGRectGetWidth(rect) - (self.indicatorHW / 2.0), 0.0))
bezierPath.addLineToPoint(CGPointMake(CGRectGetWidth(rect) - (self.indicatorHW / 2.0), CGRectGetHeight(rect)))
bezierPath.lineWidth = self.indicatorHW
}
self.indicatorColor.setStroke()
bezierPath.stroke()
}
}
}
}
// MARK: - RGTabBarItem
class RGTabBarItem: UIView {
var selected: Bool = false {
didSet {
self.setSelectedState()
}
}
var text: String?
var image: UIImage?
var textLabel: UILabel?
var imageView: UIImageView?
var normalColor: UIColor? = UIColor.grayColor()
init(frame: CGRect, text: String?, image: UIImage?, color: UIColor?) {
super.init(frame: frame)
self.text = text
self.image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
if color != nil {
self.normalColor = color
}
self.initSelf()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.initSelf()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initSelf()
}
func initSelf() {
self.backgroundColor = UIColor.clearColor()
if self.image != nil {
self.imageView = UIImageView(image: self.image)
self.addSubview(self.imageView!)
self.imageView!.tintColor = self.normalColor
self.imageView!.center.x = self.center.x
self.imageView!.center.y = self.center.y - 5.0
}
if self.text != nil {
self.textLabel = UILabel()
self.textLabel!.numberOfLines = 1
self.textLabel!.text = self.text
self.textLabel!.textAlignment = NSTextAlignment.Center
self.textLabel!.textColor = self.normalColor
self.textLabel!.font = UIFont.systemFontOfSize(10.0)
self.textLabel!.sizeToFit()
self.textLabel!.frame = CGRectMake(0.0, self.frame.size.height - self.textLabel!.frame.size.height - 3.0, self.frame.size.width, self.textLabel!.frame.size.height)
self.addSubview(self.textLabel!)
}
}
func setSelectedState() {
if self.selected {
self.textLabel?.textColor = self.tintColor
self.imageView?.tintColor = self.tintColor
} else {
self.textLabel?.textColor = self.normalColor
self.imageView?.tintColor = self.normalColor
}
}
}
// MARK: - UIImage+ColoredImage
extension UIImage {
class func coloredImage(color: UIColor, size: CGSize, opaque: Bool) -> UIImage? {
var rect: CGRect = CGRectMake(0.0, 0.0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(rect.size, opaque, 0.0)
color.setFill()
UIRectFill(rect)
var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image;
}
}
// MARK: - UINavigationBar hide Hairline
extension UINavigationBar {
func hideHairline() {
if let hairlineView: UIImageView = self.findHairlineImageView(containedIn: self) {
hairlineView.hidden = true
}
}
func findHairlineImageView(containedIn view: UIView) -> UIImageView? {
if view is UIImageView && view.bounds.size.height <= 1.0 {
return view as? UIImageView
}
for subview in view.subviews {
if let imageView: UIImageView = self.findHairlineImageView(containedIn: subview as UIView) {
return imageView
}
}
return nil
}
}
// MARK: - RGPageViewController Data Source
@objc protocol RGPageViewControllerDataSource {
/// Asks dataSource how many pages will there be.
///
/// :param: pageViewController the RGPageViewController instance that's subject to
///
/// :returns: the total number of pages
func numberOfPagesForViewController(pageViewController: RGPageViewController) -> Int
/// Asks dataSource to give a view to display as a tab item.
///
/// :param: pageViewController the RGPageViewController instance that's subject to
/// :param: index the index of the tab whose view is asked
///
/// :returns: a UIView instance that will be shown as tab at the given index
func tabViewForPageAtIndex(pageViewController: RGPageViewController, index: Int) -> UIView
/// The content for any tab. Return a UIViewController instance and RGPageViewController will use its view to show as content.
///
/// :param: pageViewController the RGPageViewController instance that's subject to
/// :param: index the index of the content whose view is asked
///
/// :returns: a UIViewController instance whose view will be shown as content
func viewControllerForPageAtIndex(pageViewController: RGPageViewController, index: Int) -> UIViewController?
}
// MARK: - RGPageViewController Delegate
@objc protocol RGPageViewControllerDelegate {
/// Delegate objects can implement this method if want to be informed when a page changed.
///
/// :param: index the index of the active page
optional func didChangePageToIndex(index: Int)
/// Delegate objects can implement this method to set a custom width for the tab view.
///
/// :returns: the width of the tab view
optional func widthForTabbar() -> CGFloat
/// Delegate objects can implement this method to set a custom height for the tab view.
///
/// :returns: the height of the tab view
optional func heightForTabbar() -> CGFloat
/// Delegate objects can implement this method to set a custom width or height for the tab indicator.
///
/// :returns: the width or height of the tab indicator
optional func widthOrHeightForIndicator() -> CGFloat
/// Delegate objects can implement this method if tabs use dynamic width.
///
/// :param: index the index of the tab
/// :returns: the width for the tab at the given index
optional func widthForTabAtIndex(index: Int) -> CGFloat
/// Delegate objects can implement this method if tabs use dynamic height.
///
/// :param: index the index of the tab
/// :returns: the height for the tab at the given index
optional func heightForTabAtIndex(index: Int) -> CGFloat
/// Delegate objects can implement this method to specify the color of the tab indicator.
///
/// :returns: the color for the tab indicator
optional func colorForTabIndicator() -> UIColor
/// Delegate objects can implement this method to specify a tint color for the tabbar.
///
/// :returns: the tint color for the tabbar
optional func tintColorForTabBar() -> UIColor?
}