RCTTabBar.m
5.9 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
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTTabBar.h"
#import "RCTEventDispatcher.h"
#import "RCTLog.h"
#import "RCTTabBarItem.h"
#import "RCTUtils.h"
#import "RCTView.h"
#import "RCTWrapperViewController.h"
#import "UIView+React.h"
@interface RCTTabBar() <UITabBarControllerDelegate>
@end
@implementation RCTTabBar
{
BOOL _tabsChanged;
UITabBarController *_tabController;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
_tabController = [UITabBarController new];
_tabController.delegate = self;
[self addSubview:_tabController.view];
}
return self;
}
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
- (UIViewController *)reactViewController
{
return _tabController;
}
- (void)dealloc
{
_tabController.delegate = nil;
[_tabController removeFromParentViewController];
}
- (void)insertReactSubview:(RCTTabBarItem *)subview atIndex:(NSInteger)atIndex
{
if (![subview isKindOfClass:[RCTTabBarItem class]]) {
RCTLogError(@"subview should be of type RCTTabBarItem");
return;
}
[super insertReactSubview:subview atIndex:atIndex];
_tabsChanged = YES;
}
- (void)removeReactSubview:(RCTTabBarItem *)subview
{
if (self.reactSubviews.count == 0) {
RCTLogError(@"should have at least one view to remove a subview");
return;
}
[super removeReactSubview:subview];
_tabsChanged = YES;
}
- (void)didUpdateReactSubviews
{
// Do nothing, as subviews are managed by `uiManagerDidPerformMounting`
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self reactAddControllerToClosestParent:_tabController];
_tabController.view.frame = self.bounds;
}
- (void)uiManagerDidPerformMounting
{
// we can't hook up the VC hierarchy in 'init' because the subviews aren't
// hooked up yet, so we do it on demand here whenever a transaction has finished
[self reactAddControllerToClosestParent:_tabController];
if (_tabsChanged) {
NSMutableArray<UIViewController *> *viewControllers = [NSMutableArray array];
for (RCTTabBarItem *tab in [self reactSubviews]) {
UIViewController *controller = tab.reactViewController;
if (!controller) {
controller = [[RCTWrapperViewController alloc] initWithContentView:tab];
}
[viewControllers addObject:controller];
}
_tabController.viewControllers = viewControllers;
_tabsChanged = NO;
}
[self.reactSubviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger index, __unused BOOL *stop) {
RCTTabBarItem *tab = (RCTTabBarItem *)view;
UIViewController *controller = self->_tabController.viewControllers[index];
if (self->_unselectedTintColor) {
[tab.barItem setTitleTextAttributes:@{NSForegroundColorAttributeName: self->_unselectedTintColor} forState:UIControlStateNormal];
}
[tab.barItem setTitleTextAttributes:@{NSForegroundColorAttributeName: self.tintColor} forState:UIControlStateSelected];
controller.tabBarItem = tab.barItem;
#if TARGET_OS_TV
// On Apple TV, disable JS control of selection after initial render
if (tab.selected && !tab.wasSelectedInJS) {
self->_tabController.selectedViewController = controller;
}
tab.wasSelectedInJS = YES;
#else
if (tab.selected) {
self->_tabController.selectedViewController = controller;
}
#endif
}];
}
- (UIColor *)barTintColor
{
return _tabController.tabBar.barTintColor;
}
- (void)setBarTintColor:(UIColor *)barTintColor
{
_tabController.tabBar.barTintColor = barTintColor;
}
- (UIColor *)tintColor
{
return _tabController.tabBar.tintColor;
}
- (void)setTintColor:(UIColor *)tintColor
{
_tabController.tabBar.tintColor = tintColor;
}
- (BOOL)translucent
{
return _tabController.tabBar.isTranslucent;
}
- (void)setTranslucent:(BOOL)translucent
{
_tabController.tabBar.translucent = translucent;
}
#if !TARGET_OS_TV
- (UIBarStyle)barStyle
{
return _tabController.tabBar.barStyle;
}
- (void)setBarStyle:(UIBarStyle)barStyle
{
_tabController.tabBar.barStyle = barStyle;
}
#endif
- (void)setUnselectedItemTintColor:(UIColor *)unselectedItemTintColor {
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
if ([_tabController.tabBar respondsToSelector:@selector(unselectedItemTintColor)]) {
_tabController.tabBar.unselectedItemTintColor = unselectedItemTintColor;
}
#endif
}
- (UITabBarItemPositioning)itemPositioning
{
#if TARGET_OS_TV
return 0;
#else
return _tabController.tabBar.itemPositioning;
#endif
}
- (void)setItemPositioning:(UITabBarItemPositioning)itemPositioning
{
#if !TARGET_OS_TV
_tabController.tabBar.itemPositioning = itemPositioning;
#endif
}
#pragma mark - UITabBarControllerDelegate
#if TARGET_OS_TV
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(nonnull UIViewController *)viewController
{
NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
RCTTabBarItem *tab = (RCTTabBarItem *)self.reactSubviews[index];
if (tab.onPress) tab.onPress(nil);
return;
}
#else
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
RCTTabBarItem *tab = (RCTTabBarItem *)self.reactSubviews[index];
if (tab.onPress) tab.onPress(nil);
return NO;
}
#endif
#if TARGET_OS_TV
- (BOOL)isUserInteractionEnabled
{
return YES;
}
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
if (context.nextFocusedView == self) {
[self becomeFirstResponder];
} else {
[self resignFirstResponder];
}
}
#endif
@end