@@ -39,6 +39,24 @@ struct TabViewImpl: View {
39
39
@ObservedObject var props : TabViewProps
40
40
var onSelect : ( _ key: String ) -> Void
41
41
42
+ var selectedActiveTintColor : UIColor ? {
43
+ // check first in selected tab
44
+ let selectedPage = props. selectedPage
45
+ if let selectedPage {
46
+ if let tabData = props. items? . tabs. findByKey ( selectedPage) {
47
+ if let activeTintColor = tabData. activeTintColor {
48
+ return RCTConvert . uiColor ( activeTintColor)
49
+ }
50
+ }
51
+ }
52
+
53
+ if let activeTintColor = props. activeTintColor {
54
+ return activeTintColor
55
+ }
56
+
57
+ return nil
58
+ }
59
+
42
60
var body : some View {
43
61
TabView ( selection: $props. selectedPage) {
44
62
ForEach ( props. children? . indices ?? 0 ..< 0 , id: \. self) { index in
@@ -63,7 +81,7 @@ struct TabViewImpl: View {
63
81
. tabBadge ( tabData? . badge)
64
82
}
65
83
}
66
- . tintColor ( props . activeTintColor )
84
+ . tintColor ( selectedActiveTintColor )
67
85
. getSidebarAdaptable ( enabled: props. sidebarAdaptable ?? false )
68
86
. onChange ( of: props. selectedPage ?? " " ) { newValue in
69
87
if ( props. disablePageAnimations) {
@@ -72,13 +90,37 @@ struct TabViewImpl: View {
72
90
UIView . setAnimationsEnabled ( true )
73
91
}
74
92
}
93
+
94
+ // to apply active tint color per tab
95
+ let scrollEdgeAppearance = configureAppearance ( for: props. scrollEdgeAppearance ?? " " )
96
+ let colorTintAppearance = configureAppearance ( appearance: scrollEdgeAppearance, inactiveTint: props. inactiveTintColor, activeTint: selectedActiveTintColor)
97
+ setupTabBarAppearance ( colorTintAppearance)
98
+
75
99
onSelect ( newValue)
76
100
}
77
101
. onChange ( of: props. scrollEdgeAppearance) { newValue in
78
- if #available( iOS 15 . 0 , * ) {
79
- UITabBar . appearance ( ) . scrollEdgeAppearance = configureAppearance ( for: newValue ?? " " )
80
- }
81
- UITabBar . appearance ( ) . standardAppearance = configureAppearance ( for: newValue ?? " " )
102
+ let scrollEdgeAppearance = configureAppearance ( for: newValue ?? " " )
103
+ let colorTintAppearance = configureAppearance ( appearance: scrollEdgeAppearance, inactiveTint: props. inactiveTintColor, activeTint: selectedActiveTintColor)
104
+
105
+ setupTabBarAppearance ( colorTintAppearance)
106
+ }
107
+ . onChange ( of: props. inactiveTintColor) { newValue in
108
+ let scrollEdgeAppearance = configureAppearance ( for: props. scrollEdgeAppearance ?? " " )
109
+ let colorTintAppearance = configureAppearance ( appearance: scrollEdgeAppearance, inactiveTint: newValue, activeTint: selectedActiveTintColor)
110
+
111
+ setupTabBarAppearance ( colorTintAppearance)
112
+ }
113
+ . onChange ( of: selectedActiveTintColor) { newValue in
114
+ let scrollEdgeAppearance = configureAppearance ( for: props. scrollEdgeAppearance ?? " " )
115
+ let colorTintAppearance = configureAppearance ( appearance: scrollEdgeAppearance, inactiveTint: props. inactiveTintColor, activeTint: newValue)
116
+
117
+ setupTabBarAppearance ( colorTintAppearance)
118
+ }
119
+ . onAppear {
120
+ // we have to keep onAppear to setup the appearance for the first render.
121
+ let scrollEdgeAppearance = configureAppearance ( for: props. scrollEdgeAppearance ?? " " )
122
+ let colorTintAppearance = configureAppearance ( appearance: scrollEdgeAppearance, inactiveTint: props. inactiveTintColor, activeTint: selectedActiveTintColor)
123
+ setupTabBarAppearance ( colorTintAppearance)
82
124
}
83
125
}
84
126
}
@@ -95,19 +137,45 @@ private func configureAppearance(for appearanceType: String) -> UITabBarAppearan
95
137
appearance. configureWithDefaultBackground ( )
96
138
}
97
139
140
+ return appearance
141
+ }
142
+
143
+ private func configureAppearance( appearance: UITabBarAppearance , inactiveTint inactiveTintColor: UIColor ? , activeTint activeTintColor: UIColor ? ) -> UITabBarAppearance {
98
144
// @see https://stackoverflow.com/a/71934882
99
- if let inactiveTintColor = props. inactiveTintColor {
100
- appearance. stackedLayoutAppearance. normal. titleTextAttributes = [ . foregroundColor: UIColor ( inactiveTintColor) ]
101
- appearance. stackedLayoutAppearance. normal. iconColor = UIColor ( inactiveTintColor)
145
+ if let inactiveTintColor {
146
+ setTabBarItemColors ( appearance. stackedLayoutAppearance, inactiveColor: inactiveTintColor)
147
+ setTabBarItemColors ( appearance. inlineLayoutAppearance, inactiveColor: inactiveTintColor)
148
+ setTabBarItemColors ( appearance. compactInlineLayoutAppearance, inactiveColor: inactiveTintColor)
102
149
}
103
- if let activeTintColor = props. activeTintColor {
104
- appearance. stackedLayoutAppearance. selected. titleTextAttributes = [ . foregroundColor: UIColor ( activeTintColor) ]
105
- appearance. stackedLayoutAppearance. selected. iconColor = UIColor ( activeTintColor)
150
+
151
+ if let activeTintColor {
152
+ setTabBarItemColors ( appearance. stackedLayoutAppearance, activeColor: activeTintColor)
153
+ setTabBarItemColors ( appearance. inlineLayoutAppearance, activeColor: activeTintColor)
154
+ setTabBarItemColors ( appearance. compactInlineLayoutAppearance, activeColor: activeTintColor)
106
155
}
107
156
108
157
return appearance
109
158
}
110
159
160
+ private func setupTabBarAppearance( _ appearance: UITabBarAppearance ) {
161
+ if #available( iOS 15 . 0 , * ) {
162
+ UITabBar . appearance ( ) . scrollEdgeAppearance = appearance
163
+ }
164
+ UITabBar . appearance ( ) . standardAppearance = appearance
165
+ }
166
+
167
+ private func setTabBarItemColors( _ itemAppearance: UITabBarItemAppearance , inactiveColor: UIColor ) {
168
+ itemAppearance. normal. iconColor = inactiveColor
169
+ itemAppearance. normal. titleTextAttributes = [ . foregroundColor: inactiveColor]
170
+ }
171
+
172
+
173
+ private func setTabBarItemColors( _ itemAppearance: UITabBarItemAppearance , activeColor: UIColor ) {
174
+ itemAppearance. selected. iconColor = activeColor
175
+ itemAppearance. selected. titleTextAttributes = [ . foregroundColor: activeColor]
176
+ }
177
+
178
+
111
179
struct TabItem : View {
112
180
var title : String ?
113
181
var icon : UIImage ?
0 commit comments