react-native/React/Views/RCTTabBarItem.m
=?UTF-8?q?=E9=9A=90=E9=A3=8E?= 7fc86dded3 [Image] Add scale support for base64-encoded image.
Summary:
Fix issue #1136
Closes https://github.com/facebook/react-native/pull/1721
Github Author: =?UTF-8?q?=E9=9A=90=E9=A3=8E?= <yinfeng.fcx@alibaba-inc.com>

Test Plan: Imported from GitHub, without a `Test Plan:` line.
2015-06-25 06:07:42 -08:00

91 lines
2.4 KiB
Objective-C

/**
* 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 "RCTTabBarItem.h"
#import "RCTConvert.h"
#import "RCTLog.h"
#import "UIView+React.h"
@implementation RCTTabBarItem
@synthesize barItem = _barItem;
- (UITabBarItem *)barItem
{
if (!_barItem) {
_barItem = [[UITabBarItem alloc] init];
}
return _barItem;
}
- (void)setIcon:(id)icon
{
static NSDictionary *systemIcons;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
systemIcons = @{
@"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),
};
});
// 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) {
// Recreate barItem if previous item was a system icon
if (wasSystemIcon) {
_barItem = nil;
self.barItem.image = image;
} else {
self.barItem.image = image;
return;
}
} else {
// 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;
}
_barItem = [[UITabBarItem alloc] initWithTabBarSystemItem:[systemIcon integerValue] tag:oldItem.tag];
}
// Reapply previous properties
_barItem.title = oldItem.title;
_barItem.imageInsets = oldItem.imageInsets;
_barItem.selectedImage = oldItem.selectedImage;
_barItem.badgeValue = oldItem.badgeValue;
}
- (UIViewController *)backingViewController
{
return self.superview.backingViewController;
}
@end