RCTTabBarItem.m
3.31 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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTTabBarItem.h"
#import "RCTConvert.h"
#import "RCTLog.h"
#import "UIView+React.h"
@implementation RCTConvert (UITabBarSystemItem)
RCT_ENUM_CONVERTER(UITabBarSystemItem, (@{
@"bookmarks": @(UITabBarSystemItemBookmarks),
@"contacts": @(UITabBarSystemItemContacts),
@"downloads": @(UITabBarSystemItemDownloads),
@"favorites": @(UITabBarSystemItemFavorites),
@"featured": @(UITabBarSystemItemFeatured),
@"history": @(UITabBarSystemItemHistory),
@"more": @(UITabBarSystemItemMore),
@"most-recent": @(UITabBarSystemItemMostRecent),
@"most-viewed": @(UITabBarSystemItemMostViewed),
@"recents": @(UITabBarSystemItemRecents),
@"search": @(UITabBarSystemItemSearch),
@"top-rated": @(UITabBarSystemItemTopRated),
}), NSNotFound, integerValue)
@end
@implementation RCTTabBarItem{
UITapGestureRecognizer *_selectRecognizer;
}
@synthesize barItem = _barItem;
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
_systemIcon = NSNotFound;
#if TARGET_OS_TV
_wasSelectedInJS = NO;
#endif
}
return self;
}
- (UITabBarItem *)barItem
{
if (!_barItem) {
_barItem = [UITabBarItem new];
_systemIcon = NSNotFound;
}
return _barItem;
}
- (void)setTestID:(NSString *)testID
{
self.barItem.accessibilityIdentifier = testID;
}
- (void)setBadge:(id)badge
{
_badge = [badge copy];
self.barItem.badgeValue = [badge description];
}
- (void)setSystemIcon:(UITabBarSystemItem)systemIcon
{
if (_systemIcon != systemIcon) {
_systemIcon = systemIcon;
UITabBarItem *oldItem = _barItem;
_barItem = [[UITabBarItem alloc] initWithTabBarSystemItem:_systemIcon
tag:oldItem.tag];
_barItem.title = oldItem.title;
_barItem.imageInsets = oldItem.imageInsets;
_barItem.badgeValue = oldItem.badgeValue;
}
}
- (void)setIcon:(UIImage *)icon
{
_icon = icon;
if (_icon && _systemIcon != NSNotFound) {
_systemIcon = NSNotFound;
UITabBarItem *oldItem = _barItem;
_barItem = [UITabBarItem new];
_barItem.title = oldItem.title;
_barItem.imageInsets = oldItem.imageInsets;
_barItem.selectedImage = oldItem.selectedImage;
_barItem.badgeValue = oldItem.badgeValue;
}
if (_renderAsOriginal) {
self.barItem.image = [_icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
} else {
self.barItem.image = _icon;
}
}
- (void)setSelectedIcon:(UIImage *)selectedIcon
{
_selectedIcon = selectedIcon;
if (_renderAsOriginal) {
self.barItem.selectedImage = [_selectedIcon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
} else {
self.barItem.selectedImage = _selectedIcon;
}
}
- (void)setBadgeColor:(UIColor *)badgeColor
{
// badgeColor available since iOS 10
if ([self.barItem respondsToSelector:@selector(badgeColor)]) {
self.barItem.badgeColor = badgeColor;
}
}
- (UIViewController *)reactViewController
{
return self.superview.reactViewController;
}
#if TARGET_OS_TV
// On Apple TV, we let native control the tab bar selection after initial render
- (void)setSelected:(BOOL)selected
{
if (!_wasSelectedInJS) {
_selected = selected;
}
}
#endif
@end