2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-03-06 00:36:41 +00:00
|
|
|
|
|
|
|
#import "RCTTabBarItem.h"
|
|
|
|
|
|
|
|
#import "RCTConvert.h"
|
|
|
|
#import "RCTLog.h"
|
2015-03-26 09:58:06 +00:00
|
|
|
#import "UIView+React.h"
|
2015-03-06 00:36:41 +00:00
|
|
|
|
|
|
|
@implementation RCTTabBarItem
|
|
|
|
|
|
|
|
@synthesize barItem = _barItem;
|
|
|
|
|
|
|
|
- (UITabBarItem *)barItem
|
|
|
|
{
|
|
|
|
if (!_barItem) {
|
2015-08-17 14:35:34 +00:00
|
|
|
_barItem = [UITabBarItem new];
|
2015-03-06 00:36:41 +00:00
|
|
|
}
|
|
|
|
return _barItem;
|
|
|
|
}
|
|
|
|
|
2015-06-25 13:00:02 +00:00
|
|
|
- (void)setIcon:(id)icon
|
2015-03-06 00:36:41 +00:00
|
|
|
{
|
|
|
|
static NSDictionary *systemIcons;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
systemIcons = @{
|
2015-04-15 00:51:28 +00:00
|
|
|
@"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),
|
|
|
|
};
|
2015-03-06 00:36:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Update icon
|
|
|
|
BOOL wasSystemIcon = (systemIcons[_icon] != nil);
|
|
|
|
_icon = [icon copy];
|
|
|
|
|
|
|
|
// Check if string matches any custom images first
|
|
|
|
UIImage *image = [RCTConvert UIImage:_icon];
|
|
|
|
UITabBarItem *oldItem = _barItem;
|
|
|
|
if (image) {
|
2015-10-06 14:42:59 +00:00
|
|
|
// Recreate barItem if previous item was a system icon. Calling self.barItem
|
|
|
|
// creates a new instance if it wasn't set yet.
|
2015-03-06 00:36:41 +00:00
|
|
|
if (wasSystemIcon) {
|
|
|
|
_barItem = nil;
|
|
|
|
self.barItem.image = image;
|
|
|
|
} else {
|
|
|
|
self.barItem.image = image;
|
|
|
|
return;
|
|
|
|
}
|
2015-10-06 14:42:59 +00:00
|
|
|
} else if ([icon isKindOfClass:[NSString class]] && [icon length] > 0) {
|
2015-03-06 00:36:41 +00:00
|
|
|
// Not a custom image, may be a system item?
|
|
|
|
NSNumber *systemIcon = systemIcons[icon];
|
|
|
|
if (!systemIcon) {
|
|
|
|
RCTLogError(@"The tab bar icon '%@' did not match any known image or system icon", icon);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-24 10:14:33 +00:00
|
|
|
_barItem = [[UITabBarItem alloc] initWithTabBarSystemItem:systemIcon.integerValue tag:oldItem.tag];
|
2015-10-06 14:42:59 +00:00
|
|
|
} else {
|
|
|
|
self.barItem.image = nil;
|
2015-03-06 00:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reapply previous properties
|
|
|
|
_barItem.title = oldItem.title;
|
|
|
|
_barItem.imageInsets = oldItem.imageInsets;
|
|
|
|
_barItem.selectedImage = oldItem.selectedImage;
|
|
|
|
_barItem.badgeValue = oldItem.badgeValue;
|
|
|
|
}
|
|
|
|
|
2015-07-31 18:23:29 +00:00
|
|
|
- (UIViewController *)reactViewController
|
2015-03-06 00:36:41 +00:00
|
|
|
{
|
2015-07-31 18:23:29 +00:00
|
|
|
return self.superview.reactViewController;
|
2015-03-06 00:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|