mirror of
https://github.com/status-im/react-native.git
synced 2025-01-16 04:24:15 +00:00
50c2467905
Summary: Hi, This PR Solves this issue #3083. This PR solves the problem of default color on TabBar being always grey. Which looks great if the barTintColor is unchanged. However if we set the barTintColor to something else (like blue in example) text and icons become quite unreadable. ![simulator screen shot 27 apr 2016 21 58 40](https://cloud.githubusercontent.com/assets/12081272/14866402/e51c7120-0cc3-11e6-9570-097b686c160f.png) Commit (c206417) - Enable setting color of unselected tabs Solves this issue with a prop (unselectedTintColor) on TabBarIOS to which you just pass a color like you can for barTintColor and tintColor. This leaves us with a result that is on second picture. Notice the color of text on tabs. ![simulator screen shot 27 apr 2016 21 59 06](https://cloud.githubusercontent.com/assets/12081272/14866419/f77aa7e2-0cc3-11e6-8c90-33209009bc09.png) Or change it to yellow for demonstrating purposes ![simulator screen shot 27 apr 2016 21 59 13](https://cloud.githubusercontent.com/assets/1208 Closes https://github.com/facebook/react-native/pull/7264 Differential Revision: D3240924 Pulled By: nicklockwood fb-gh-sync-id: 14a0de28abd064756320b7a74f128c255caa6b12 fbshipit-source-id: 14a0de28abd064756320b7a74f128c255caa6b12
164 lines
4.1 KiB
Objective-C
164 lines
4.1 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 "RCTTabBar.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
#import "RCTLog.h"
|
|
#import "RCTTabBarItem.h"
|
|
#import "RCTUtils.h"
|
|
#import "RCTView.h"
|
|
#import "RCTViewControllerProtocol.h"
|
|
#import "RCTWrapperViewController.h"
|
|
#import "UIView+React.h"
|
|
|
|
@interface RCTTabBar() <UITabBarControllerDelegate>
|
|
|
|
@end
|
|
|
|
@implementation RCTTabBar
|
|
{
|
|
BOOL _tabsChanged;
|
|
UITabBarController *_tabController;
|
|
NSMutableArray<RCTTabBarItem *> *_tabViews;
|
|
}
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
if ((self = [super initWithFrame:frame])) {
|
|
_tabViews = [NSMutableArray new];
|
|
_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];
|
|
}
|
|
|
|
- (NSArray<RCTTabBarItem *> *)reactSubviews
|
|
{
|
|
return _tabViews;
|
|
}
|
|
|
|
- (void)insertReactSubview:(RCTTabBarItem *)view atIndex:(NSInteger)atIndex
|
|
{
|
|
if (![view isKindOfClass:[RCTTabBarItem class]]) {
|
|
RCTLogError(@"subview should be of type RCTTabBarItem");
|
|
return;
|
|
}
|
|
[_tabViews insertObject:view atIndex:atIndex];
|
|
_tabsChanged = YES;
|
|
}
|
|
|
|
- (void)removeReactSubview:(RCTTabBarItem *)subview
|
|
{
|
|
if (_tabViews.count == 0) {
|
|
RCTLogError(@"should have at least one view to remove a subview");
|
|
return;
|
|
}
|
|
[_tabViews removeObject:subview];
|
|
_tabsChanged = YES;
|
|
}
|
|
|
|
- (void)layoutSubviews
|
|
{
|
|
[super layoutSubviews];
|
|
[self reactAddControllerToClosestParent:_tabController];
|
|
_tabController.view.frame = self.bounds;
|
|
}
|
|
|
|
- (void)reactBridgeDidFinishTransaction
|
|
{
|
|
// 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;
|
|
}
|
|
|
|
[_tabViews enumerateObjectsUsingBlock:
|
|
^(RCTTabBarItem *tab, NSUInteger index, __unused BOOL *stop) {
|
|
UIViewController *controller = _tabController.viewControllers[index];
|
|
if (_unselectedTintColor) {
|
|
[tab.barItem setTitleTextAttributes:@{NSForegroundColorAttributeName: _unselectedTintColor} forState:UIControlStateNormal];
|
|
}
|
|
|
|
[tab.barItem setTitleTextAttributes:@{NSForegroundColorAttributeName: self.tintColor} forState:UIControlStateSelected];
|
|
|
|
controller.tabBarItem = tab.barItem;
|
|
if (tab.selected) {
|
|
_tabController.selectedViewController = controller;
|
|
}
|
|
}];
|
|
}
|
|
|
|
- (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;
|
|
}
|
|
|
|
#pragma mark - UITabBarControllerDelegate
|
|
|
|
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
|
|
{
|
|
NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
|
|
RCTTabBarItem *tab = _tabViews[index];
|
|
if (tab.onPress) tab.onPress(nil);
|
|
return NO;
|
|
}
|
|
|
|
@end
|