Skip to content

Commit

Permalink
Restore bottomTabs visibility when needed (#6298)
Browse files Browse the repository at this point in the history
Until now, `tabBar.frame` got restored each time child appeared which resulted in wrong bottomTabs visibility.
In this PR we changed it so the `tabBar.frame` will get restored only after the tabBar frame has changed through `mergeOptions`.

Closes #6282
  • Loading branch information
yogevbd committed Jun 14, 2020
1 parent 5b7356d commit fe024ea
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 54 deletions.
12 changes: 12 additions & 0 deletions e2e/BottomTabs.test.js
Expand Up @@ -75,4 +75,16 @@ describe('BottomTabs', () => {
await elementById(TestIDs.POP_BTN).tap();
await expect(elementById(TestIDs.BOTTOM_TABS)).toBeVisible();
});

it('hide Tab Bar on push from second bottomTabs screen - deep stack', async () => {
await elementById(TestIDs.SWITCH_TAB_BY_INDEX_BTN).tap();
await elementById(TestIDs.HIDE_TABS_PUSH_BTN).tap();
await expect(elementById(TestIDs.BOTTOM_TABS)).toBeNotVisible();
await elementById(TestIDs.PUSH_BTN).tap();
await expect(elementById(TestIDs.BOTTOM_TABS)).toBeVisible();
await elementById(TestIDs.POP_BTN).tap();
await expect(elementById(TestIDs.BOTTOM_TABS)).toBeNotVisible();
await elementById(TestIDs.POP_BTN).tap();
await expect(elementById(TestIDs.BOTTOM_TABS)).toBeVisible();
});
});
11 changes: 6 additions & 5 deletions lib/ios/BottomTabsBasePresenter.m
@@ -1,4 +1,5 @@
#import "BottomTabsBasePresenter.h"
#import "RNNBottomTabsController.h"

@implementation BottomTabsBasePresenter

Expand All @@ -13,11 +14,11 @@ - (void)applyOptionsOnInit:(RNNNavigationOptions *)options {
}

- (void)applyOptions:(RNNNavigationOptions *)options {
UITabBarController *bottomTabs = self.tabBarController;
RNNBottomTabsController *bottomTabs = self.tabBarController;
RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];

[bottomTabs setTabBarTestID:[withDefault.bottomTabs.testID getWithDefaultValue:nil]];
![withDefault.bottomTabs.visible getWithDefaultValue:YES] ?: [bottomTabs setTabBarVisible:[withDefault.bottomTabs.visible getWithDefaultValue:YES] animated:[withDefault.bottomTabs.animate getWithDefaultValue:NO]];
[bottomTabs restoreTabBarVisibility:[withDefault.bottomTabs.visible getWithDefaultValue:YES]];

[bottomTabs.view setBackgroundColor:[withDefault.layout.backgroundColor getWithDefaultValue:nil]];
[self applyBackgroundColor:[withDefault.bottomTabs.backgroundColor getWithDefaultValue:nil] translucent:[withDefault.bottomTabs.translucent getWithDefaultValue:NO]];
Expand All @@ -27,7 +28,7 @@ - (void)applyOptions:(RNNNavigationOptions *)options {

- (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigationOptions *)currentOptions {
[super mergeOptions:options resolvedOptions:currentOptions];
UITabBarController *bottomTabs = self.tabBarController;
RNNBottomTabsController *bottomTabs = self.tabBarController;

if (options.bottomTabs.currentTabIndex.hasValue) {
[bottomTabs setCurrentTabIndex:options.bottomTabs.currentTabIndex.get];
Expand Down Expand Up @@ -72,8 +73,8 @@ - (void)mergeOptions:(RNNNavigationOptions *)options resolvedOptions:(RNNNavigat
}
}

- (UITabBarController *)tabBarController {
return (UITabBarController *)self.boundViewController;
- (RNNBottomTabsController *)tabBarController {
return (RNNBottomTabsController *)self.boundViewController;
}

- (UITabBar *)tabBar {
Expand Down
2 changes: 1 addition & 1 deletion lib/ios/RNNBasePresenter.m
Expand Up @@ -120,7 +120,7 @@ - (BOOL)getStatusBarVisibility {
}

- (BOOL)hidesBottomBarWhenPushed {
RNNNavigationOptions *withDefault = (RNNNavigationOptions *)[[self.boundViewController.topMostViewController.resolveOptions withDefault:self.defaultOptions] mergeOptions:self.boundViewController.options];
RNNNavigationOptions *withDefault = (RNNNavigationOptions *)[self.boundViewController.topMostViewController.resolveOptions withDefault:self.defaultOptions];
return ![withDefault.bottomTabs.visible getWithDefaultValue:YES];
}

Expand Down
4 changes: 4 additions & 0 deletions lib/ios/RNNBottomTabsController.h
Expand Up @@ -21,6 +21,10 @@

- (void)setSelectedIndexByComponentID:(NSString *)componentID;

- (void)setTabBarVisible:(BOOL)visible animated:(BOOL)animated;

- (void)restoreTabBarVisibility:(BOOL)visible;

@property (nonatomic, strong) NSArray* pendingChildViewControllers;

@end
13 changes: 13 additions & 0 deletions lib/ios/RNNBottomTabsController.m
Expand Up @@ -10,6 +10,7 @@ @interface RNNBottomTabsController ()
@implementation RNNBottomTabsController {
NSUInteger _currentTabIndex;
BottomTabsBaseAttacher* _bottomTabsAttacher;
BOOL _tabBarNeedsRestore;

}

Expand Down Expand Up @@ -110,6 +111,18 @@ - (void)loadChildren:(NSArray *)children {
}
}

- (void)setTabBarVisible:(BOOL)visible animated:(BOOL)animated {
_tabBarNeedsRestore = YES;
visible ? [self showTabBar:animated] : [self hideTabBar:animated];
}

- (void)restoreTabBarVisibility:(BOOL)visible {
if (_tabBarNeedsRestore) {
[self setTabBarVisible:visible animated:NO];
_tabBarNeedsRestore = NO;
}
}

#pragma mark UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
Expand Down
6 changes: 4 additions & 2 deletions lib/ios/UITabBarController+RNNOptions.h
Expand Up @@ -14,8 +14,10 @@

- (void)setTabBarHideShadow:(BOOL)hideShadow;

- (void)setTabBarVisible:(BOOL)visible animated:(BOOL)animated;

- (void)centerTabItems;

- (void)showTabBar:(BOOL)animated;

- (void)hideTabBar:(BOOL)animated;

@end
87 changes: 45 additions & 42 deletions lib/ios/UITabBarController+RNNOptions.m
Expand Up @@ -28,51 +28,54 @@ - (void)setTabBarHideShadow:(BOOL)hideShadow {
self.tabBar.clipsToBounds = hideShadow;
}

- (void)setTabBarVisible:(BOOL)visible animated:(BOOL)animated {
const CGRect tabBarFrame = self.tabBar.frame;
const CGRect tabBarVisibleFrame = CGRectMake(tabBarFrame.origin.x,
self.view.frame.size.height - tabBarFrame.size.height,
tabBarFrame.size.width,
tabBarFrame.size.height);
const CGRect tabBarHiddenFrame = CGRectMake(tabBarFrame.origin.x,
self.view.frame.size.height,
tabBarFrame.size.width,
tabBarFrame.size.height);
if (!animated) {
self.tabBar.hidden = !visible;
self.tabBar.frame = visible ? tabBarVisibleFrame : tabBarHiddenFrame;
return;
}
static const CGFloat animationDuration = 0.15;
- (void)centerTabItems {
[self.tabBar centerTabItems];
}


if (visible) {
self.tabBar.hidden = NO;
[UIView animateWithDuration: animationDuration
delay: 0
options: UIViewAnimationOptionCurveEaseOut
animations:^()
{
self.tabBar.frame = tabBarVisibleFrame;
}
completion:^(BOOL finished)
{}];
} else {
[UIView animateWithDuration: animationDuration
delay: 0
options: UIViewAnimationOptionCurveEaseIn
animations:^()
{
self.tabBar.frame = tabBarHiddenFrame;
}
completion:^(BOOL finished)
{
self.tabBar.hidden = YES;
}];
}
- (void)showTabBar:(BOOL)animated {
static const CGFloat animationDuration = 0.15;
const CGRect tabBarVisibleFrame = CGRectMake(self.tabBar.frame.origin.x,
self.view.frame.size.height - self.tabBar.frame.size.height,
self.tabBar.frame.size.width,
self.tabBar.frame.size.height);
self.tabBar.hidden = NO;
if (!animated) {
self.tabBar.frame = tabBarVisibleFrame;
} else {
[UIView animateWithDuration: animationDuration
delay: 0
options: UIViewAnimationOptionCurveEaseOut
animations:^()
{
self.tabBar.frame = tabBarVisibleFrame;
} completion:^(BOOL finished)
{}];
}
}

- (void)centerTabItems {
[self.tabBar centerTabItems];
- (void)hideTabBar:(BOOL)animated {
static const CGFloat animationDuration = 0.15;
const CGRect tabBarHiddenFrame = CGRectMake(self.tabBar.frame.origin.x,
self.view.frame.size.height,
self.tabBar.frame.size.width,
self.tabBar.frame.size.height);

if (!animated) {
self.tabBar.frame = tabBarHiddenFrame;
self.tabBar.hidden = YES;
} else {
[UIView animateWithDuration: animationDuration
delay: 0
options: UIViewAnimationOptionCurveEaseOut
animations:^()
{
self.tabBar.frame = tabBarHiddenFrame;
} completion:^(BOOL finished)
{
self.tabBar.hidden = YES;
}];
}
}

- (void)forEachTab:(void (^)(UIView *, UIViewController * tabViewController, int tabIndex))performOnTab {
Expand Down
Expand Up @@ -28,7 +28,7 @@ - (void)testApplyOptions_shouldSetDefaultEmptyOptions {
[[(id)self.uut expect] applyBackgroundColor:nil translucent:NO];
[[self.boundViewController expect] setTabBarHideShadow:NO];
[[self.boundViewController expect] setTabBarStyle:UIBarStyleDefault];
[[self.boundViewController expect] setTabBarVisible:YES animated:NO];
[[self.boundViewController expect] restoreTabBarVisibility:YES];
[self.uut applyOptions:emptyOptions];
[self.boundViewController verify];
}
Expand All @@ -55,7 +55,7 @@ - (void)testApplyOptions_shouldRestoreHiddenTabBar {
RNNNavigationOptions *initialOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
initialOptions.bottomTabs.visible = [[Bool alloc] initWithValue:@(1)];

[[self.boundViewController expect] setTabBarVisible:YES animated:NO];
[[self.boundViewController expect] restoreTabBarVisibility:YES];

[self.uut applyOptions:initialOptions];
[self.boundViewController verify];
Expand Down
Expand Up @@ -36,7 +36,7 @@ - (void)testApplyOptions_shouldSetDefaultEmptyOptions {
[[self.boundViewController expect] setTabBarTestID:nil];
[[(id)self.uut expect] applyBackgroundColor:nil translucent:NO];
[[self.boundViewController expect] setTabBarHideShadow:NO];
[[self.boundViewController expect] setTabBarVisible:YES animated:NO];
[[self.boundViewController expect] restoreTabBarVisibility:YES];
[[self.boundViewController expect] setTabBarStyle:UIBarStyleDefault];
[self.uut applyOptions:emptyOptions];
[self.boundViewController verify];
Expand Down Expand Up @@ -64,7 +64,7 @@ - (void)testApplyOptions_shouldRestoreHiddenTabBar {
RNNNavigationOptions *initialOptions = [[RNNNavigationOptions alloc] initEmptyOptions];
initialOptions.bottomTabs.visible = [[Bool alloc] initWithValue:@(1)];

[[self.boundViewController expect] setTabBarVisible:YES animated:NO];
[[self.boundViewController expect] restoreTabBarVisibility:YES];

[self.uut applyOptions:initialOptions];
[self.boundViewController verify];
Expand Down

0 comments on commit fe024ea

Please sign in to comment.