-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTabBarController.swift
More file actions
196 lines (162 loc) · 5.07 KB
/
TabBarController.swift
File metadata and controls
196 lines (162 loc) · 5.07 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
//
// TabBarController.swift
// RubyEvents
//
// Created by Marco Roth on 11.01.2025.
//
import HotwireNative
import UIKit
struct TabBarConfigurationItem {
let title: String
let icon: String
let url: URL
let position: Int
}
struct TabBarConfiguration {
let items: [TabBarConfigurationItem]
}
class TabBarController: UITabBarController {
var app: App
var configuration: TabBarConfiguration?
var navigators: [Navigator] = []
var isSetup: Bool = false
init(app: App) {
self.app = app
super.init(nibName: nil, bundle: nil)
}
func fixedTabBarItemFrom(item: TabBarConfigurationItem) -> FixedTabBarItem {
FixedTabBarItem(
title: item.title,
image: UIImage(systemName: item.icon),
tag: item.position
)
}
func reloadAllTabs() {
self.navigators.forEach { navigator in
reloadTab(navigator: navigator)
}
}
func reloadTab(title: String) {
if let navigator = app.navigatorFor(title: title) {
reloadTab(navigator: navigator)
}
}
func reloadTab(navigator: Navigator) {
navigator.rootViewController.popToRootViewController(animated: false)
navigator.activeWebView.reload()
}
func navigatorFor(title: String) -> Navigator? {
let index = configuration?.items.firstIndex { $0.self.title == title }
guard let index else { return nil }
return navigators[index]
}
var currentNavigator: Navigator? {
navigatorFor(title: currentTabTitle ?? "")
}
var currentTabTitle: String? {
self.tabBar.selectedItem?.title
}
func hideNavigationBarFor(title: String) {
let navigator = self.navigatorFor(title: title)
navigator?.rootViewController.navigationBar.isHidden = true
}
func showNavigationBarFor(title: String) {
let navigator = self.navigatorFor(title: title)
navigator?.rootViewController.navigationBar.isHidden = false
}
func hideTabBar() {
self.tabBar.isHidden = true
}
func showTabBar() {
self.tabBar.isHidden = false
}
func setupTabs() {
setup(
configuration: TabBarConfiguration(
items: [
TabBarConfigurationItem(
title: "Home",
icon: "house",
url: Router.instance.home_url(),
position: 0
),
TabBarConfigurationItem(
title: "Events",
icon: "calendar",
url: Router.instance.events_url(),
position: 1
),
TabBarConfigurationItem(
title: "Talks",
icon: "music.mic",
url: Router.instance.talks_url(),
position: 2
),
TabBarConfigurationItem(
title: "Speakers",
icon: "person.fill",
url: Router.instance.speakers_url(),
position: 3
)
]
)
)
}
func setup(configuration: TabBarConfiguration) {
guard !isSetup else { return }
self.configuration = configuration
var tabBarItems: [FixedTabBarItem] = []
configuration.items.forEach { item in
let navigator = Navigator(delegate: app)
self.navigators.append(navigator)
navigator.route(item.url)
tabBarItems.append(fixedTabBarItemFrom(item: item))
}
viewControllers = navigators.map(\.rootViewController)
tabBarItems.enumerated().forEach { index, item in
if let viewController = self.viewControllers?[index] {
// viewController.tabBarController?.tabBar.tintColor = .white
// viewController.tabBarController?.tabBar.barTintColor = .black
viewController.tabBarItem = item
}
}
// NavBar styling
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
UINavigationBar.appearance().isOpaque = false
UINavigationBar.appearance().isTranslucent = true
navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.black]
// TabBar styling
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.configureWithDefaultBackground()
tabBarAppearance.backgroundColor = UIColor.white
UITabBar.appearance().standardAppearance = tabBarAppearance
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
UITabBar.appearance().isOpaque = true
UITabBar.appearance().isTranslucent = false
UITabBar.appearance().tintColor = .red
UITabBar.appearance().unselectedItemTintColor = .black
self.isSetup = true
}
func reset() {
self.isSetup = false
self.configuration = nil
self.navigators = []
self.viewControllers = nil
self.selectedIndex = 0
}
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
// self.tabBar.backgroundColor = .white
// self.tabBar.barStyle = .default
// self.tabBar.isTranslucent = false
// self.tabBar.isOpaque = false
// self.tabBar.barTintColor = .white
// self.tabBar.tintColor = .gray
}
}