-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCaseContainerView.swift
More file actions
199 lines (164 loc) · 7.37 KB
/
CaseContainerView.swift
File metadata and controls
199 lines (164 loc) · 7.37 KB
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
//
// CaseContainerView.swift
// CaseContainer
//
// Created by minjuniMac on 25/09/2018.
// Copyright © 2018 mjun. All rights reserved.
//
import UIKit
open class CaseContainerView: CaseContainerBaseView<CaseContainerViewController> {
public private(set) var containerScrollView = UIScrollView().then {
$0.backgroundColor = .red
$0.showsHorizontalScrollIndicator = false
$0.showsVerticalScrollIndicator = false
/// was specified unidirection Scrolling, this direction of scrollView is vertical
$0.isDirectionalLockEnabled = true
$0.bounces = true
/// height of scrollView's contentSize is adjusted in iPhoneX' seriess(iPhoneX, iPhone XS, iPhone XR...). I didn't want to this Behavior
if #available(iOS 11, *) {
$0.contentInsetAdjustmentBehavior = .never
}
}
/**
This property is containerScrollView's contentView
*/
lazy public private(set) var verticalCanvasView = UIView().then {
$0.backgroundColor = .brown
}
public var tabScrollView = TabScrollView()
public var contentsScrollView = UIScrollView().then {
$0.bounces = true
$0.backgroundColor = .purple
$0.isDirectionalLockEnabled = true
$0.showsVerticalScrollIndicator = false
$0.isPagingEnabled = true
}
/**
This propery is ContentsScrollView's Content View
*/
lazy public private(set) var horizonCanvasView = UIView().then {
let rect = CGRect(
x: 0, y: 0,
width: ui.contentsScrollViewContentSize.width,
height: ui.contentsScrollViewContentSize.height)
$0.frame = rect
$0.backgroundColor = .blue
}
public var tabBarHeight: CGFloat {
guard let homeIndicator = UIApplication.shared.windows.first,
let tabBarController = vc.tabBarController else {
return 0
}
let tabBarHeight = tabBarController.tabBar.frame.height
if #available(iOS 11.0, *) {
let homeIndicatorHeight = homeIndicator.safeAreaInsets.bottom
return tabBarHeight + homeIndicatorHeight
} else {
return tabBarHeight
}
}
struct UI {
var tabScrollViewHeight: CGFloat
var contentsScrollViewFrameSize: CGSize
var contentsScrollViewContentSize: CGSize
var containerScrollViewContentSize: CGSize
init(containerViewController: CaseContainerViewController,
tabScrollHeaight: CGFloat,
tabBarHeight: CGFloat) {
guard containerViewController.viewContorllers.count > 0 else {
fatalError("you must Implement ChildViewController")
}
let numberOfChildVierControlelr: CGFloat = CGFloat(containerViewController.viewContorllers.count)
tabScrollViewHeight = tabScrollHeaight
contentsScrollViewFrameSize = CGSize(
width: UIScreen.mainWidth,
height: UIScreen.mainHeight - (UIApplication.statusBarHeight + tabScrollViewHeight + tabBarHeight))
contentsScrollViewContentSize = CGSize(
width: UIScreen.mainWidth * numberOfChildVierControlelr,
height: UIScreen.mainHeight - UIApplication.statusBarHeight )
containerScrollViewContentSize = CGSize(
width: UIScreen.mainWidth,
height: tabScrollViewHeight + contentsScrollViewFrameSize.height)
}
}
lazy var ui = UI(
containerViewController: vc,
tabScrollHeaight: vc.appearence.tabScrollHegiht,
tabBarHeight: tabBarHeight)
override func setupUI() {
backgroundColor = .white
/*
⎮ View
⎮ -- containerScrollView
⎮ ---- verticalCanvasView
⎮ ------ headerView
⎮ ------ tabCollectionView
⎮ ------ contentsScrollView
⎮ -------- horizonCanvasView
⎮ ---------- vc.children.view
⎮ ---------- vc.children.view1
⎮ ----------- ...
*/
addSubviews([containerScrollView])
containerScrollView.addSubview(verticalCanvasView)
contentsScrollView.addSubview(horizonCanvasView)
verticalCanvasView.addSubviews([tabScrollView, contentsScrollView])
containerScrollView
.topAnchor(to: layoutMarginsGuide.topAnchor)
.leadingAnchor(to: leadingAnchor)
.trailingAnchor(to: trailingAnchor)
.bottomAnchor(to: bottomAnchor, constant: -tabBarHeight)
.activateAnchors()
verticalCanvasView
.topAnchor(to: containerScrollView.topAnchor)
.bottomAnchor(to: containerScrollView.bottomAnchor)
.leadingAnchor(to: containerScrollView.leadingAnchor)
.trailingAnchor(to: containerScrollView.trailingAnchor)
.dimensionAnchors(size: ui.containerScrollViewContentSize)
.activateAnchors()
tabScrollView
.topAnchor(to: verticalCanvasView.topAnchor)
.leadingAnchor(to: verticalCanvasView.leadingAnchor)
.trailingAnchor(to: verticalCanvasView.trailingAnchor)
.heightAnchor(constant: ui.tabScrollViewHeight)
.activateAnchors()
contentsScrollView
.topAnchor(to: tabScrollView.bottomAnchor)
.leadingAnchor(to: verticalCanvasView.leadingAnchor)
.trailingAnchor(to: verticalCanvasView.trailingAnchor)
.heightAnchor(constant: ui.contentsScrollViewFrameSize.height)
.activateAnchors()
/// ViewControllers that are added on initilization add parent ViewController's childViewController
guard vc.viewContorllers.count > 0 else {
fatalError("You must be to add container view controller's child view controller when it is initializaed")
}
vc.viewContorllers.forEach {
guard $0.title != nil else {
fatalError("You must be to add viewControllers`s title when it is initialized")
}
}
setupChildViewController(of: vc.viewContorllers)
}
override func setupBinding() {
containerScrollView.contentSize = ui.containerScrollViewContentSize
contentsScrollView.contentSize = ui.contentsScrollViewContentSize
containerScrollView.delegate = vc
contentsScrollView.delegate = vc
tabScrollView.delegate = vc
tabScrollView.setupBinding(parent: vc, ui: ui)
}
func setupChildViewController(of childViewControllers: [UIViewController]) {
guard let initialChildVC = childViewControllers.first else {
return
}
// 1. Add child Viewcontroller into ContainerViewControlelr
vc.addChild(initialChildVC)
// 2. add child ViewController's view into ContainerViewController view
horizonCanvasView.addSubview(initialChildVC.view)
// 3. determine child View Controller`s view frame
initialChildVC.view.frame = CGRect(x: 0, y: 0,
width: ui.contentsScrollViewFrameSize.width,
height: ui.contentsScrollViewContentSize.height)
initialChildVC.didMove(toParent: vc)
}
}