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 "RCTTabBar.h"
|
|
|
|
|
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTTabBarItem.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
#import "RCTView.h"
|
|
|
|
#import "RCTViewControllerProtocol.h"
|
|
|
|
#import "RCTWrapperViewController.h"
|
2015-03-26 09:58:06 +00:00
|
|
|
#import "UIView+React.h"
|
2015-03-06 00:36:41 +00:00
|
|
|
|
|
|
|
@interface RCTTabBar() <UITabBarControllerDelegate>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTTabBar
|
|
|
|
{
|
|
|
|
BOOL _tabsChanged;
|
|
|
|
UITabBarController *_tabController;
|
|
|
|
}
|
|
|
|
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
2015-03-06 00:36:41 +00:00
|
|
|
{
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
if ((self = [super initWithFrame:frame])) {
|
2015-08-17 14:35:34 +00:00
|
|
|
_tabController = [UITabBarController new];
|
2015-03-06 00:36:41 +00:00
|
|
|
_tabController.delegate = self;
|
|
|
|
[self addSubview:_tabController.view];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-07-31 18:23:29 +00:00
|
|
|
- (UIViewController *)reactViewController
|
2015-03-06 00:36:41 +00:00
|
|
|
{
|
|
|
|
return _tabController;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
_tabController.delegate = nil;
|
2015-11-05 16:57:50 +00:00
|
|
|
[_tabController removeFromParentViewController];
|
2015-03-06 00:36:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 07:08:16 +00:00
|
|
|
- (void)insertReactSubview:(RCTTabBarItem *)subview atIndex:(NSInteger)atIndex
|
2015-03-06 00:36:41 +00:00
|
|
|
{
|
2016-06-07 07:08:16 +00:00
|
|
|
if (![subview isKindOfClass:[RCTTabBarItem class]]) {
|
2015-03-06 00:36:41 +00:00
|
|
|
RCTLogError(@"subview should be of type RCTTabBarItem");
|
|
|
|
return;
|
|
|
|
}
|
2016-06-07 07:08:16 +00:00
|
|
|
[super insertReactSubview:subview atIndex:atIndex];
|
2015-03-06 00:36:41 +00:00
|
|
|
_tabsChanged = YES;
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
- (void)removeReactSubview:(RCTTabBarItem *)subview
|
2015-03-06 00:36:41 +00:00
|
|
|
{
|
2016-06-07 07:08:16 +00:00
|
|
|
if (self.reactSubviews.count == 0) {
|
2015-03-06 00:36:41 +00:00
|
|
|
RCTLogError(@"should have at least one view to remove a subview");
|
|
|
|
return;
|
|
|
|
}
|
2016-06-07 07:08:16 +00:00
|
|
|
[super removeReactSubview:subview];
|
2015-03-06 00:36:41 +00:00
|
|
|
_tabsChanged = YES;
|
|
|
|
}
|
|
|
|
|
2016-06-07 07:08:16 +00:00
|
|
|
- (void)didUpdateReactSubviews
|
|
|
|
{
|
|
|
|
// Do nothing, as subviews are managed by `reactBridgeDidFinishTransaction`
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
- (void)layoutSubviews
|
|
|
|
{
|
|
|
|
[super layoutSubviews];
|
2015-10-23 10:14:05 +00:00
|
|
|
[self reactAddControllerToClosestParent:_tabController];
|
2015-03-06 00:36:41 +00:00
|
|
|
_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
|
2015-07-31 18:23:29 +00:00
|
|
|
[self reactAddControllerToClosestParent:_tabController];
|
2015-03-06 00:36:41 +00:00
|
|
|
|
|
|
|
if (_tabsChanged) {
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<UIViewController *> *viewControllers = [NSMutableArray array];
|
2015-03-06 00:36:41 +00:00
|
|
|
for (RCTTabBarItem *tab in [self reactSubviews]) {
|
2015-07-31 18:23:29 +00:00
|
|
|
UIViewController *controller = tab.reactViewController;
|
2015-03-06 00:36:41 +00:00
|
|
|
if (!controller) {
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
controller = [[RCTWrapperViewController alloc] initWithContentView:tab];
|
2015-03-06 00:36:41 +00:00
|
|
|
}
|
|
|
|
[viewControllers addObject:controller];
|
|
|
|
}
|
|
|
|
|
|
|
|
_tabController.viewControllers = viewControllers;
|
|
|
|
_tabsChanged = NO;
|
|
|
|
}
|
|
|
|
|
2016-06-07 07:08:16 +00:00
|
|
|
[self.reactSubviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger index, __unused BOOL *stop) {
|
|
|
|
|
|
|
|
RCTTabBarItem *tab = (RCTTabBarItem *)view;
|
2016-07-07 19:36:56 +00:00
|
|
|
UIViewController *controller = self->_tabController.viewControllers[index];
|
|
|
|
if (self->_unselectedTintColor) {
|
|
|
|
[tab.barItem setTitleTextAttributes:@{NSForegroundColorAttributeName: self->_unselectedTintColor} forState:UIControlStateNormal];
|
2016-05-03 12:39:21 +00:00
|
|
|
}
|
2016-05-21 00:17:29 +00:00
|
|
|
|
2016-05-03 12:39:21 +00:00
|
|
|
[tab.barItem setTitleTextAttributes:@{NSForegroundColorAttributeName: self.tintColor} forState:UIControlStateSelected];
|
2016-05-21 00:17:29 +00:00
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
controller.tabBarItem = tab.barItem;
|
|
|
|
if (tab.selected) {
|
2016-07-07 19:36:56 +00:00
|
|
|
self->_tabController.selectedViewController = controller;
|
2015-03-06 00:36:41 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (UIColor *)barTintColor
|
|
|
|
{
|
|
|
|
return _tabController.tabBar.barTintColor;
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:40:56 +00:00
|
|
|
- (void)setBarTintColor:(UIColor *)barTintColor
|
|
|
|
{
|
|
|
|
_tabController.tabBar.barTintColor = barTintColor;
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (UIColor *)tintColor
|
|
|
|
{
|
|
|
|
return _tabController.tabBar.tintColor;
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:40:56 +00:00
|
|
|
- (void)setTintColor:(UIColor *)tintColor
|
|
|
|
{
|
|
|
|
_tabController.tabBar.tintColor = tintColor;
|
|
|
|
}
|
|
|
|
|
2015-07-14 15:05:08 +00:00
|
|
|
- (BOOL)translucent {
|
|
|
|
return _tabController.tabBar.isTranslucent;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setTranslucent:(BOOL)translucent {
|
|
|
|
_tabController.tabBar.translucent = translucent;
|
|
|
|
}
|
|
|
|
|
2016-05-21 00:17:29 +00:00
|
|
|
- (UITabBarItemPositioning)itemPositoning
|
|
|
|
{
|
2016-09-27 13:19:45 +00:00
|
|
|
#if TARGET_OS_TV
|
|
|
|
return 0;
|
|
|
|
#else
|
2016-05-21 00:17:29 +00:00
|
|
|
return _tabController.tabBar.itemPositioning;
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2016-05-21 00:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setItemPositioning:(UITabBarItemPositioning)itemPositioning
|
|
|
|
{
|
2016-09-27 13:19:45 +00:00
|
|
|
#if !TARGET_OS_TV
|
2016-05-21 00:17:29 +00:00
|
|
|
_tabController.tabBar.itemPositioning = itemPositioning;
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2016-05-21 00:17:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
#pragma mark - UITabBarControllerDelegate
|
|
|
|
|
|
|
|
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
|
|
|
|
{
|
|
|
|
NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
|
2016-06-07 07:08:16 +00:00
|
|
|
RCTTabBarItem *tab = (RCTTabBarItem *)self.reactSubviews[index];
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
if (tab.onPress) tab.onPress(nil);
|
2015-03-06 00:36:41 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|