From a63601ef1f28a7eb637adf85aba313bd05e81a16 Mon Sep 17 00:00:00 2001 From: yogevbd Date: Tue, 16 Jan 2018 14:25:52 +0200 Subject: [PATCH 1/2] fixes #2524 --- .../layouts/BottomTabsLayout.java | 11 ++- .../params/parsers/StyleParamsParser.java | 8 ++ .../reactnativenavigation/screens/Screen.java | 2 +- .../views/BottomTabs.java | 50 +++++----- ios/RCCTabBarController.h | 1 + ios/RCCTabBarController.m | 6 +- ios/RCCViewController.m | 92 ++++++++++++++++++- 7 files changed, 130 insertions(+), 40 deletions(-) diff --git a/android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java b/android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java index 6c7be214981..79ad3c02ec6 100644 --- a/android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java +++ b/android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java @@ -16,6 +16,7 @@ import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.WritableMap; import com.reactnativenavigation.NavigationApplication; +import com.reactnativenavigation.events.Event; import com.reactnativenavigation.events.EventBus; import com.reactnativenavigation.events.ScreenChangedEvent; import com.reactnativenavigation.params.ActivityParams; @@ -229,6 +230,8 @@ public void updateScreenStyle(String screenInstanceId, Bundle styleParams) { for (int i = 0; i < bottomTabs.getItemsCount(); i++) { screenStacks[i].updateScreenStyle(screenInstanceId, styleParams); } + + bottomTabs.setStyleFromScreen(this.getCurrentScreen().getStyleParams()); } @Override @@ -360,7 +363,7 @@ public void run(ScreenStack param) { private boolean hasBackgroundColor(StyleParams params) { return params.screenBackgroundColor != null && - params.screenBackgroundColor.hasColor(); + params.screenBackgroundColor.hasColor(); } private void setStyleFromScreen(StyleParams params) { @@ -391,7 +394,7 @@ public void pop(final ScreenParams params) { performOnStack(params.getNavigatorId(), new Task() { @Override public void run(ScreenStack stack) { - stack.pop(params.animateScreenTransitions, params.timestamp, new ScreenStack.OnScreenPop() { + stack.pop(params.animateScreenTransitions, params.timestamp, new ScreenStack.OnScreenPop() { @Override public void onScreenPopAnimationEnd() { setBottomTabsStyleFromCurrentScreen(); @@ -450,8 +453,8 @@ private void performOnStack(String navigatorId, Task task) { task.run(screenStack); } catch (ScreenStackNotFoundException e) { Log.e("Navigation", "Could not perform action on stack [" + navigatorId + "]." + - "This should not have happened, it probably means a navigator action" + - "was called from an unmounted tab."); + "This should not have happened, it probably means a navigator action" + + "was called from an unmounted tab."); } } diff --git a/android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java b/android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java index 0d295b91245..e33e11c908d 100644 --- a/android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java +++ b/android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java @@ -131,6 +131,14 @@ private StyleParams createDefaultStyleParams() { result.titleBarHeight = -1; result.screenAnimationType = "slide-up"; result.drawUnderStatusBar = false; + + result.bottomTabsColor = getDefaultBottomTabsColor(); + result.bottomTabsButtonColor = getDefaultBottomTabsButtonColor(); + result.selectedBottomTabsButtonColor = getDefaultSelectedBottomTabsButtonColor(); + + result.bottomTabBadgeTextColor = getBottomTabBadgeTextColor(); + result.bottomTabBadgeBackgroundColor = getBottomTabBadgeBackgroundColor(); + return result; } diff --git a/android/app/src/main/java/com/reactnativenavigation/screens/Screen.java b/android/app/src/main/java/com/reactnativenavigation/screens/Screen.java index 010212e6ad6..0d1f5ae9aa7 100644 --- a/android/app/src/main/java/com/reactnativenavigation/screens/Screen.java +++ b/android/app/src/main/java/com/reactnativenavigation/screens/Screen.java @@ -216,7 +216,7 @@ public void setTopBarVisible(boolean visible, boolean animate) { } public void setTitleBarTitle(String title) { - topBar.setTitle(title, styleParams); + topBar.setTitle(title, styleParams); } public void setTitleBarSubtitle(String subtitle) { diff --git a/android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java b/android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java index 7ca224e6edc..0a9858daaf5 100644 --- a/android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java +++ b/android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java @@ -2,6 +2,7 @@ import android.content.Context; import android.graphics.Color; +import android.os.Bundle; import android.text.TextUtils; import com.aurelhubert.ahbottomnavigation.AHBottomNavigation; @@ -10,6 +11,7 @@ import com.reactnativenavigation.params.AppStyle; import com.reactnativenavigation.params.ScreenParams; import com.reactnativenavigation.params.StyleParams; +import com.reactnativenavigation.params.parsers.StyleParamsParser; import com.reactnativenavigation.utils.ViewUtils; import com.reactnativenavigation.views.utils.Constants; @@ -21,17 +23,17 @@ public class BottomTabs extends AHBottomNavigation { public BottomTabs(Context context) { super(context); + setForceTint(true); setId(ViewUtils.generateViewId()); createVisibilityAnimator(); - setStyle(); + setStyle(AppStyle.appStyle); setFontFamily(); } public void addTabs(List params, OnTabSelectedListener onTabSelectedListener) { for (ScreenParams screenParams : params) { - AHBottomNavigationItem item = new AHBottomNavigationItem(screenParams.tabLabel, screenParams.tabIcon, - Color.GRAY); + AHBottomNavigationItem item = new AHBottomNavigationItem(screenParams.tabLabel, screenParams.tabIcon, Color.GRAY); addItem(item); setOnTabSelectedListener(onTabSelectedListener); } @@ -39,19 +41,7 @@ public void addTabs(List params, OnTabSelectedListener onTabSelect } public void setStyleFromScreen(StyleParams params) { - if (params.bottomTabsColor.hasColor()) { - setBackgroundColor(params.bottomTabsColor); - } - if (params.bottomTabsButtonColor.hasColor()) { - if (getInactiveColor() != params.bottomTabsButtonColor.getColor()) { - setInactiveColor(params.bottomTabsButtonColor.getColor()); - } - } - if (params.selectedBottomTabsButtonColor.hasColor()) { - if (getAccentColor() != params.selectedBottomTabsButtonColor.getColor()) { - setAccentColor(params.selectedBottomTabsButtonColor.getColor()); - } - } + this.setStyle(params); setVisibility(params.bottomTabsHidden, true); } @@ -132,23 +122,27 @@ private void createVisibilityAnimator() { Constants.BOTTOM_TABS_HEIGHT); } - private void setStyle() { - if (hasBadgeBackgroundColor()) { + private void setStyle(StyleParams params) { + if (params.bottomTabBadgeBackgroundColor.hasColor()) { setNotificationBackgroundColor(AppStyle.appStyle.bottomTabBadgeBackgroundColor.getColor()); } - if (hasBadgeTextColor()) { + if (params.bottomTabBadgeTextColor.hasColor()) { setNotificationTextColor(AppStyle.appStyle.bottomTabBadgeTextColor.getColor()); } - } - private boolean hasBadgeTextColor() { - return AppStyle.appStyle.bottomTabBadgeTextColor != null && - AppStyle.appStyle.bottomTabBadgeTextColor.hasColor(); - } - - private boolean hasBadgeBackgroundColor() { - return AppStyle.appStyle.bottomTabBadgeBackgroundColor != null && - AppStyle.appStyle.bottomTabBadgeBackgroundColor.hasColor(); + if (params.bottomTabsColor.hasColor()) { + setBackgroundColor(params.bottomTabsColor); + } + if (params.bottomTabsButtonColor.hasColor()) { + if (getInactiveColor() != params.bottomTabsButtonColor.getColor()) { + setInactiveColor(params.bottomTabsButtonColor.getColor()); + } + } + if (params.selectedBottomTabsButtonColor.hasColor()) { + if (getAccentColor() != params.selectedBottomTabsButtonColor.getColor()) { + setAccentColor(params.selectedBottomTabsButtonColor.getColor()); + } + } } private void setFontFamily() { diff --git a/ios/RCCTabBarController.h b/ios/RCCTabBarController.h index 9274fec4b17..a65e3cb4298 100755 --- a/ios/RCCTabBarController.h +++ b/ios/RCCTabBarController.h @@ -5,6 +5,7 @@ - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge; - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge completion:(void (^)(void))completion; ++ (UIImage *)image:(UIImage*)image withColor:(UIColor *)color1; @property (nonatomic) BOOL tabBarHidden; diff --git a/ios/RCCTabBarController.m b/ios/RCCTabBarController.m index 7f5568bc4fb..1f271f46466 100755 --- a/ios/RCCTabBarController.m +++ b/ios/RCCTabBarController.m @@ -55,7 +55,7 @@ - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectView return YES; } -- (UIImage *)image:(UIImage*)image withColor:(UIColor *)color1 ++ (UIImage *)image:(UIImage*)image withColor:(UIColor *)color1 { UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale); CGContextRef context = UIGraphicsGetCurrentContext(); @@ -159,7 +159,7 @@ - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children iconImage = [RCTConvert UIImage:icon]; if (buttonColor) { - iconImage = [[self image:iconImage withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; + iconImage = [[RCCTabBarController image:iconImage withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } } UIImage *iconImageSelected = nil; @@ -327,7 +327,7 @@ - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actio if (icon && icon != (id)[NSNull null]) { iconImage = [RCTConvert UIImage:icon]; - iconImage = [[self image:iconImage withColor:self.tabBar.tintColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; + iconImage = [[RCCTabBarController image:iconImage withColor:self.tabBar.tintColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; viewController.tabBarItem.image = iconImage; } diff --git a/ios/RCCViewController.m b/ios/RCCViewController.m index dee2dcc7f37..9d68fcdda5c 100755 --- a/ios/RCCViewController.m +++ b/ios/RCCViewController.m @@ -494,11 +494,95 @@ -(void)setStyleOnAppearForViewController:(UIViewController*)viewController appea [viewController setNeedsStatusBarAppearanceUpdate]; } - NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"]; - BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO; - if (tabBarHiddenBool) { + if (viewController.tabBarController && viewController.tabBarController.tabBar != (id)[NSNull null]) { UITabBar *tabBar = viewController.tabBarController.tabBar; - tabBar.transform = CGAffineTransformMakeTranslation(0, tabBar.frame.size.height); + + if (tabBar && tabBar != (id)[NSNull null]) { + UIColor *buttonColor = nil; + UIColor *selectedButtonColor = nil; + UIColor *labelColor = nil; + UIColor *selectedLabelColor = nil; + + NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"]; + BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO; + if (tabBarHiddenBool) { + tabBar.transform = CGAffineTransformMakeTranslation(0, tabBar.frame.size.height); + } + + NSString *tabBarButtonColor = self.navigatorStyle[@"tabBarButtonColor"]; + NSString *tabBarSelectedButtonColor = self.navigatorStyle[@"tabBarSelectedButtonColor"]; + + if (tabBarButtonColor) + { + buttonColor = tabBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarButtonColor] : nil; + + if (tabBarSelectedButtonColor) { + selectedButtonColor = tabBarSelectedButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarSelectedButtonColor] : nil; + + tabBar.tintColor = selectedLabelColor = selectedButtonColor; + tabBar.unselectedItemTintColor = labelColor = buttonColor; + } + else { + tabBar.tintColor = labelColor = buttonColor; + } + } + else if (tabBarSelectedButtonColor) { + selectedButtonColor = tabBarSelectedButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarSelectedButtonColor] : nil; + tabBar.tintColor = selectedLabelColor = selectedButtonColor; + } + + NSString *tabBarLabelColor = self.navigatorStyle[@"tabBarLabelColor"]; + if(tabBarLabelColor) { + UIColor *color = tabBarLabelColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarLabelColor] : nil; + labelColor = color; + } + NSString *tabBarSelectedLabelColor = self.navigatorStyle[@"tabBarSelectedLabelColor"]; + if(tabBarLabelColor) { + UIColor *color = tabBarSelectedLabelColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarSelectedLabelColor] : nil; + selectedLabelColor = color; + } + + NSString *tabBarBackgroundColor = self.navigatorStyle[@"tabBarBackgroundColor"]; + if (tabBarBackgroundColor) + { + UIColor *color = tabBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarBackgroundColor] : nil; + tabBar.barTintColor = color; + } + + NSNumber *tabBarTranslucent = self.navigatorStyle[@"tabBarTranslucent"]; + if (tabBarTranslucent) + { + BOOL tabBarTranslucentBool = tabBarTranslucent ? [tabBarTranslucent boolValue] : NO; + tabBar.translucent = tabBarTranslucentBool; + } + + NSNumber *tabBarHideShadow = self.navigatorStyle[@"tabBarHideShadow"]; + if (tabBarHideShadow) + { + BOOL tabBarHideShadowBool = tabBarHideShadow ? [tabBarHideShadow boolValue] : NO; + tabBar.clipsToBounds = tabBarHideShadowBool ? YES : NO; + } + + for (UIViewController *tabViewController in [viewController.tabBarController viewControllers]) { + NSMutableDictionary *unselectedAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"tabBarText" baseFont:[UIFont systemFontOfSize:10]]; + if (!unselectedAttributes[NSForegroundColorAttributeName] && labelColor) { + unselectedAttributes[NSForegroundColorAttributeName] = labelColor; + } + [tabViewController.tabBarItem setTitleTextAttributes:unselectedAttributes forState:UIControlStateNormal]; + + + NSMutableDictionary *selectedAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"tabBarSelectedText" baseFont:[UIFont systemFontOfSize:10]]; + if (!selectedAttributes[NSForegroundColorAttributeName] && selectedLabelColor) { + selectedAttributes[NSForegroundColorAttributeName] = selectedLabelColor; + } + [tabViewController.tabBarItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected]; + + if (buttonColor) + { + tabViewController.tabBarItem.image = [[RCCTabBarController image:tabViewController.tabBarItem.image withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; + } + } + } } NSNumber *navBarHidden = self.navigatorStyle[@"navBarHidden"]; From 98977d7151fc6b6f5ee8bf7393617b9427fcff0c Mon Sep 17 00:00:00 2001 From: yogevbd Date: Tue, 16 Jan 2018 14:57:38 +0200 Subject: [PATCH 2/2] fix in RNNNavigationController --- ios/RCCNavigationController.m | 36 ++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/ios/RCCNavigationController.m b/ios/RCCNavigationController.m index 116c6505a26..05508549163 100755 --- a/ios/RCCNavigationController.m +++ b/ios/RCCNavigationController.m @@ -339,23 +339,25 @@ - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actio // setStyle if ([performAction isEqualToString:@"setStyle"]) { - - NSDictionary *navigatorStyle = actionParams; - - // merge the navigatorStyle of our parent - if ([self.topViewController isKindOfClass:[RCCViewController class]]) - { - RCCViewController *parent = (RCCViewController*)self.topViewController; - NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle]; - - // there are a few styles that we don't want to remember from our parent (they should be local) - [mergedStyle setValuesForKeysWithDictionary:navigatorStyle]; - navigatorStyle = mergedStyle; - - parent.navigatorStyle = navigatorStyle; - - [parent setStyleOnInit]; - [parent updateStyle]; + for (UIViewController *viewController in self.viewControllers) { + if ([viewController isKindOfClass:[RCCViewController class]]) + { + RCCViewController *rccViewController = (RCCViewController*)viewController; + + NSDictionary *navigatorStyle = [[NSDictionary alloc] initWithDictionary:actionParams copyItems:YES]; + NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:rccViewController.navigatorStyle]; + + // there are a few styles that we don't want to remember from our parent (they should be local) + [mergedStyle setValuesForKeysWithDictionary:navigatorStyle]; + + rccViewController.navigatorStyle = mergedStyle; + + [rccViewController setStyleOnInit]; + + if (viewController == self.topViewController) { + [rccViewController updateStyle]; + } + } } } }