-
Notifications
You must be signed in to change notification settings - Fork 433
Expand file tree
/
Copy pathExamplesViewController.swift
More file actions
167 lines (152 loc) · 5.67 KB
/
ExamplesViewController.swift
File metadata and controls
167 lines (152 loc) · 5.67 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
import UIKit
enum Example: CaseIterable {
case basic
case selfSizing
case calendar
case sizeDelegate
case wheel
case images
case icons
case storyboard
case navigationBar
case largeTitles
case scroll
case header
case multipleCells
case pageViewController
var title: String {
switch self {
case .basic:
return "Basic"
case .selfSizing:
return "Self sizing cells"
case .calendar:
return "Calendar"
case .sizeDelegate:
return "Size delegate"
case .wheel:
return "Wheel"
case .images:
return "Images"
case .icons:
return "Icons"
case .storyboard:
return "Storyboard"
case .navigationBar:
return "Navigation bar"
case .largeTitles:
return "Large titles"
case .scroll:
return "Hide menu on scroll"
case .header:
return "Header above menu"
case .multipleCells:
return "Multiple cells"
case .pageViewController:
return "PageViewController"
}
}
}
final class ExamplesViewController: UITableViewController {
static let CellIdentifier = "CellIdentifier"
var isUITesting: Bool {
return ProcessInfo.processInfo.arguments.contains("--ui-testing")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: Self.CellIdentifier)
if isUITesting {
let viewController = createViewController(for: .basic)
navigationController?.setViewControllers([viewController], animated: false)
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Self.CellIdentifier, for: indexPath)
let example = Example.allCases[indexPath.row]
cell.textLabel?.text = example.title
return cell
}
override func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
return Example.allCases.count
}
override func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
let example = Example.allCases[indexPath.row]
let viewController = createViewController(for: example)
viewController.title = example.title
switch example {
case .largeTitles:
let navigationController = NavigationController(rootViewController: viewController)
navigationController.modalPresentationStyle = .fullScreen
viewController.navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .done,
target: self,
action: #selector(handleDismiss)
)
present(navigationController, animated: true)
default:
viewController.view.backgroundColor = .white
navigationController?.pushViewController(viewController, animated: true)
}
}
private func createViewController(for example: Example) -> UIViewController {
switch example {
case .basic:
return BasicViewController(nibName: nil, bundle: nil)
case .calendar:
return CalendarViewController(nibName: nil, bundle: nil)
case .selfSizing:
return SelfSizingViewController()
case .sizeDelegate:
return SizeDelegateViewController(nibName: nil, bundle: nil)
case .wheel:
return WheelViewController()
case .images:
return UnsplashViewController(nibName: nil, bundle: nil)
case .icons:
return IconsViewController(nibName: nil, bundle: nil)
case .storyboard:
return StoryboardViewController(nibName: nil, bundle: nil)
case .navigationBar:
return NavigationBarViewController(nibName: nil, bundle: nil)
case .largeTitles:
return LargeTitlesViewController(nibName: nil, bundle: nil)
case .scroll:
return ScrollViewController(nibName: nil, bundle: nil)
case .header:
return HeaderViewController(nibName: nil, bundle: nil)
case .multipleCells:
return MultipleCellsViewController(nibName: nil, bundle: nil)
case .pageViewController:
return PageViewExampleViewController(nibName: nil, bundle: nil)
}
}
@objc private func handleDismiss() {
dismiss(animated: true)
}
}
final class NavigationController: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .systemBlue
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navigationBar.tintColor = .white
navigationBar.standardAppearance = appearance
navigationBar.compactAppearance = appearance
navigationBar.scrollEdgeAppearance = appearance
}
// For debugging purposes. Adds a hook to push a new view
// controller to debug navigation controller related issues.
override func motionBegan(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
let viewController = UIViewController()
viewController.title = "Page"
viewController.view.backgroundColor = .white
pushViewController(viewController, animated: true)
}
}
}