-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseViewController.swift
More file actions
executable file
·380 lines (334 loc) · 16.7 KB
/
Copy pathBaseViewController.swift
File metadata and controls
executable file
·380 lines (334 loc) · 16.7 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
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
import UIKit
class BaseViewController: UIViewController {
// MARK: - typealias
typealias ModalPresentationStyle = UIModalPresentationStyle
typealias ModalTransitionStyle = UIModalTransitionStyle
// MARK: - Variables
/// 取得當前畫面的 UIViewController
var topVisibleViewController: UIViewController? {
if #available(iOS 15, *) {
guard let navigationController = UIApplication.shared
.connectedScenes
.compactMap({ $0 as? UIWindowScene })
.flatMap({ $0.windows })
.first(where: { $0.isKeyWindow })?.rootViewController as? UINavigationController else {
return nil
}
return navigationController.visibleViewController
} else {
guard let navigationController = UIApplication.shared
.windows
.first(where: { $0.isKeyWindow })?.rootViewController as? UINavigationController else {
return nil
}
return navigationController.visibleViewController
}
}
/// 取得當前畫面的 UINavigationController
var rootNavigationController: UINavigationController? {
if #available(iOS 15, *) {
guard let navigationController = UIApplication.shared
.connectedScenes
.compactMap({ $0 as? UIWindowScene })
.flatMap({ $0.windows })
.first(where: { $0.isKeyWindow })?.rootViewController as? UINavigationController else {
return nil
}
return navigationController
} else {
guard let navigationController = UIApplication.shared
.windows
.first(where: { $0.isKeyWindow })?.rootViewController as? UINavigationController else {
return nil
}
return navigationController
}
}
// MARK: - NavigationBar Style
/// 設定 NavigationBar 的顏色
/// - Parameters:
/// - backgroundColor: navigationBar 的背景色
/// - tintColor: navigationBar 上 items 的色調顏色,預設為 UIColor.white
/// - foregroundColor: navigationBar 上的文字顏色,預設為 UIColor.white
public func setupNavigationBarStyle(backgroundColor: UIColor,
tintColor: UIColor = .white,
foregroundColor: UIColor = .white) {
if #available(iOS 15.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = backgroundColor
self.navigationController?.navigationBar.tintColor = tintColor
appearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor : foregroundColor
]
self.navigationController?.navigationBar.standardAppearance = appearance
self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
} else {
self.navigationController?.navigationBar.barTintColor = backgroundColor
self.navigationController?.navigationBar.tintColor = tintColor
self.navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.foregroundColor : foregroundColor
]
}
}
// MARK: - NavigationController.present
/// NavigationController.presentViewController 跳頁 (帶 Closure)
/// - Parameters:
/// - viewController: 要跳頁到的 UIViewController
/// - animated: 是否要換頁動畫,預設為 true
/// - isFullScreen: 是否全螢幕顯示,預設為 false
/// - completion: 換頁過程中,要做的事,預設為 nil
public func presentViewController(viewController: UIViewController,
animated: Bool = true,
isFullScreen: Bool = false,
completion: (() -> Void)? = nil) {
let navigationVC = UINavigationController(rootViewController: viewController)
navigationVC.modalPresentationStyle = (isFullScreen) ? .fullScreen : .automatic
self.navigationController?.present(navigationVC, animated: animated, completion: completion)
}
// MARK: - NavigationController.push
/// NavigationController.pushViewController 跳頁 (不帶 Closure)
///
/// 一般常見的 self.navigationController.pushViewController
///
/// - Parameters:
/// - nextVC: 要跳頁到的 UIViewController
/// - animated: 是否要換頁動畫,預設為 true
public func pushViewController(nextVC: UIViewController, animated: Bool = true) {
self.navigationController?.pushViewController(nextVC, animated: animated)
}
/// NavigationController.pushViewController 跳頁 (不帶 Closure)
///
/// 用 UIApplication.shared.connectScenes 裡面第一個是 keyWindow 的 rootViewController 來當作 UINavigationController,來進行 pushViewController
///
/// - Parameters:
/// - viewController: 要跳頁到的 UIViewController
/// - animated: 是否要換頁動畫,預設為 true
public func pushViewController(_ viewController: UIViewController, animated: Bool = true) {
if #available(iOS 15.0, *) {
if let navigationController = UIApplication.shared
.connectedScenes
.compactMap({ $0 as? UIWindowScene })
.flatMap({ $0.windows })
.first(where: { $0.isKeyWindow })?.rootViewController as? UINavigationController {
navigationController.pushViewController(viewController, animated: animated)
}
} else {
if let navigationController = UIApplication.shared
.windows
.first(where: { $0.isKeyWindow })?.rootViewController as? UINavigationController {
navigationController.pushViewController(viewController, animated: animated)
}
}
}
/// NavigationController.pushViewController 跳頁 (帶 Closure)
/// - Parameters:
/// - viewController: 要跳頁到的 UIViewController
/// - animated: 是否要換頁動畫
/// - completion: 換頁過程中,要做的事
public func pushViewController(_ viewController: UIViewController,
animated: Bool,
completion: @escaping () -> Void) {
self.navigationController?.pushViewController(viewController, animated: animated)
guard animated, let coordinator = transitionCoordinator else {
DispatchQueue.main.async { completion() }
return
}
coordinator.animate(alongsideTransition: nil) { _ in completion() }
}
/// NavigationController.pushViewController 跳頁時刪掉舊的畫面 (不帶 Closure)
/// - Parameters:
/// - currentVC: 現在的 UIViewController
/// - nextVC: 要跳頁到的 UIViewController
/// - animated: 是否要換頁動畫
public func pushViewController(currentVC: UIViewController,
nextVC: UIViewController,
animated: Bool) {
navigationController?.pushViewController(nextVC, animated: true)
guard self.navigationController != nil && self.navigationController?.viewControllers != nil else {
return
}
// 刪除上一個畫面
let arrayVC = NSMutableArray(array: (self.navigationController?.viewControllers)!)
for vc in arrayVC {
if (vc as! UIViewController) == currentVC {
arrayVC.remove(vc)
break
}
}
self.navigationController?.viewControllers = arrayVC as! [UIViewController]
}
/// NavigationController.pushViewController 跳頁時刪掉舊的畫面 (帶 Closure)
/// - Parameters:
/// - currentVC: 現在的 UIViewController
/// - nextVC: 要跳頁到的 UIViewController
/// - animated: 是否要換頁動畫
/// - completion: 換頁過程中,要做的事
public func pushViewController(currentVC: UIViewController,
nextVC: UIViewController,
animated: Bool,
completion: @escaping () -> Void) {
navigationController?.pushViewController(nextVC, animated: true)
guard self.navigationController != nil && self.navigationController?.viewControllers != nil else {
return
}
// 刪除上一個畫面
let arrayVC = NSMutableArray(array: (self.navigationController?.viewControllers)!)
for vc in arrayVC {
if (vc as! UIViewController) == currentVC {
arrayVC.remove(vc)
break
}
}
self.navigationController?.viewControllers = arrayVC as! [UIViewController]
guard animated, let coordinator = transitionCoordinator else {
DispatchQueue.main.async {
completion()
}
return
}
coordinator.animate(alongsideTransition: nil) { _ in
completion()
}
}
// MARK: - NavigationController.pop
/// NavigationController.popViewController 回上一頁 (不帶 Closure)
/// - Parameters:
/// - animated: 是否要換頁動畫,預設為 true
public func popViewController(_ animated: Bool = true) {
self.navigationController?.popViewController(animated: animated)
}
/// NavigationController.popViewController 回上一頁 (帶 Closure)
/// - Parameters:
/// - animated: 是否要換頁動畫
/// - completion: 換頁過程中,要做的事
public func popViewController(animated: Bool, completion: @escaping () -> Void) {
self.navigationController?.popViewController(animated: animated)
guard animated, let coordinator = transitionCoordinator else {
DispatchQueue.main.async { completion() }
return
}
coordinator.animate(alongsideTransition: nil) { _ in completion() }
}
/// NavigationController.popToViewController 回到指定 ViewController (不帶 Closure)
/// - Parameters:
/// - currentVC: 目前所在的 ViewController
/// - popVC_index: 在 NavigationController.viewControllers 中,指定 ViewController 的 index
/// - animated: 是否要換頁動畫,預設為 true
public func popToViewController(currentVC viewController: UIViewController,
popVC_index: Int,
animated: Bool = true) {
guard let currentVC_index = navigationController?.viewControllers.firstIndex(of: self) else { return }
if let vc = navigationController?.viewControllers[currentVC_index - popVC_index] {
self.navigationController?.popToViewController(vc, animated: animated)
}
}
/// NavigationController.popToViewController 回到指定 ViewController (不帶 Closure)
///
/// 一般常見的 self.navigationController?.popToViewController
///
/// - Parameters:
/// - viewController: 要 pop 回去的 ViewController
/// - animated: 是否要換頁動畫,預設為 true
public func popToViewController(_ viewController: UIViewController,
animated: Bool = true) {
self.navigationController?.popToViewController(viewController, animated: animated)
}
/// NavigationController.popToViewController 回到指定 ViewController (帶 Closure)
/// - Parameters:
/// - viewController: 要 pop 回去的 ViewController
/// - animated: 是否要換頁動畫
/// - completion: 換頁過程中,要做的事
public func popToViewController(_ viewController: UIViewController,
animated: Bool,
completion: @escaping () -> Void) {
self.navigationController?.popToViewController(viewController, animated: animated)
guard animated, let coordinator = transitionCoordinator else {
DispatchQueue.main.async { completion() }
return
}
coordinator.animate(alongsideTransition: nil) { _ in completion() }
}
/// NavigationController.popToRootViewController 回到 Root ViewController
/// - Parameters:
/// - animated: 是否要換頁動畫,預設為 true
public func popToRootViewController(_ animated: Bool = true) {
self.navigationController?.popToRootViewController(animated: animated)
}
// MARK: - NavigationController.dismiss
/// NavigationController.dismiss (帶 Closure)
/// - Parameters:
/// - animated: 是否要關閉動畫,預設為 true
/// - completion: 關閉過程中,要做的事,預設為 nil
public func dismissViewController(_ animated: Bool = true, completion: (()-> Void)? = nil) {
self.navigationController?.dismiss(animated: animated, completion: completion)
}
// MARK: - ViewController.popUp
/// ViewController.popUp (帶 Closure)
/// - Parameters:
/// - viewController: 要彈出的 ViewController
/// - modalPresentationStyle: UIModalPresentationStyle,預設為 .overFullScreen
/// - modalTransitionStyle: UIModalTransitionStyle,預設為 .coverVertical
/// - animated: 是否要彈出動畫,預設為 true
/// - completion: 彈出過程中,要做的事,預設為 nil
public func popUpViewController(viewController: UIViewController,
modalPresentationStyle: ModalPresentationStyle = .overFullScreen,
modalTransitionStyle: ModalTransitionStyle = .coverVertical,
animated: Bool = true,
completion: (()-> Void)? = nil) {
viewController.modalPresentationStyle = modalPresentationStyle
viewController.modalTransitionStyle = modalTransitionStyle
self.present(viewController, animated: animated, completion: completion)
}
/// ViewController.dismissPopUp
/// - Parameters:
/// - animated: 是否要關閉動畫,預設為 true
/// - completion: 關閉過程中,要做的事,預設為 nil
public func dismissPopUpViewController(_ animated: Bool = true, completion: (()-> Void)? = nil) {
self.dismiss(animated: animated, completion: completion)
}
/// 清除 NavigationStack 中除了下一個畫面以外的畫面
/// - Parameters:
/// - viewController: 下一個畫面的 ViewController
public func cleanViewControllers(viewController: UIViewController) {
guard viewController.navigationController?.viewControllers != nil else {
return
}
var vcs = viewController.navigationController!.viewControllers
for _ in 0 ..< vcs.count - 1 {
vcs.remove(at: 0)
}
DispatchQueue.main.async {
viewController.navigationController!.viewControllers = vcs
}
}
// MARK: - Create TabBarController
/// 建立主畫面底下的 UITabBarController
/// - Parameters:
/// - vcs: 要加入 TabBarController 的 Controller
/// - vcTitleArray: 各頁面的 Title 陣列
/// - imageNameArray: 各頁面 icon 的系統圖片名稱陣列,UIImage(systemName: )
/// - modelPresentationStyle: 呈現樣式,預設為 UIModalPresentationStyle.fullScreen
/// - Returns: 建立好的 UITabBarController
public func createTabBarController(vcs: [UIViewController],
vcTitleArray: [String],
imageNameArray: [String],
modelPresentationStyle: ModalPresentationStyle = .fullScreen) -> UITabBarController {
let tabBarVC = UITabBarController()
let navVC1 = UINavigationController(rootViewController: vcs[0])
let navVC2 = UINavigationController(rootViewController: vcs[1])
let navVC3 = UINavigationController(rootViewController: vcs[2])
navVC1.title = vcTitleArray[0]
navVC2.title = vcTitleArray[1]
navVC3.title = vcTitleArray[2]
let navVCArray = [navVC1, navVC2, navVC3]
tabBarVC.setViewControllers(navVCArray, animated: false)
if let items = tabBarVC.tabBar.items {
for x in 0 ..< items.count {
items[x].image = UIImage(systemName: imageNameArray[x])
}
}
tabBarVC.modalPresentationStyle = modelPresentationStyle
return tabBarVC
}
}