Browse Source

setStyle to style the according TabBar (#2224)

setTabButton -> has now the property 'label' to change the label of the TabButton

iOS update style-update rutine to update each viewcontroller in a NavigationViewController
tabButtonColor improvement
Daniel Lang 6 years ago
parent
commit
076ee7311e

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/BottomTabsLayout.java View File

229
         for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
229
         for (int i = 0; i < bottomTabs.getItemsCount(); i++) {
230
             screenStacks[i].updateScreenStyle(screenInstanceId, styleParams);
230
             screenStacks[i].updateScreenStyle(screenInstanceId, styleParams);
231
         }
231
         }
232
+
233
+        bottomTabs.updateTabStyle(styleParams);
232
     }
234
     }
233
 
235
 
234
     @Override
236
     @Override

+ 8
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java View File

126
         result.titleBarButtonFontFamily = new StyleParams.Font();
126
         result.titleBarButtonFontFamily = new StyleParams.Font();
127
         result.titleBarHeight = -1;
127
         result.titleBarHeight = -1;
128
         result.screenAnimationType = "slide-up";
128
         result.screenAnimationType = "slide-up";
129
+
130
+        result.bottomTabsColor = getDefaultBottomTabsColor();
131
+        result.bottomTabsButtonColor = getDefaultBottomTabsButtonColor();
132
+        result.selectedBottomTabsButtonColor = getDefaultSelectedBottomTabsButtonColor();
133
+
134
+        result.bottomTabBadgeTextColor = getBottomTabBadgeTextColor();
135
+        result.bottomTabBadgeBackgroundColor = getBottomTabBadgeBackgroundColor();
136
+
129
         return result;
137
         return result;
130
     }
138
     }
131
 
139
 

+ 25
- 26
android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java View File

2
 
2
 
3
 import android.content.Context;
3
 import android.content.Context;
4
 import android.graphics.Color;
4
 import android.graphics.Color;
5
+import android.os.Bundle;
5
 import android.text.TextUtils;
6
 import android.text.TextUtils;
6
 
7
 
7
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
8
 import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
10
 import com.reactnativenavigation.params.AppStyle;
11
 import com.reactnativenavigation.params.AppStyle;
11
 import com.reactnativenavigation.params.ScreenParams;
12
 import com.reactnativenavigation.params.ScreenParams;
12
 import com.reactnativenavigation.params.StyleParams;
13
 import com.reactnativenavigation.params.StyleParams;
14
+import com.reactnativenavigation.params.parsers.StyleParamsParser;
13
 import com.reactnativenavigation.utils.ViewUtils;
15
 import com.reactnativenavigation.utils.ViewUtils;
14
 import com.reactnativenavigation.views.utils.Constants;
16
 import com.reactnativenavigation.views.utils.Constants;
15
 
17
 
24
         setForceTint(true);
26
         setForceTint(true);
25
         setId(ViewUtils.generateViewId());
27
         setId(ViewUtils.generateViewId());
26
         createVisibilityAnimator();
28
         createVisibilityAnimator();
27
-        setStyle();
29
+        setStyle(AppStyle.appStyle);
28
         setFontFamily();
30
         setFontFamily();
29
     }
31
     }
30
 
32
 
38
         setTitlesDisplayState();
40
         setTitlesDisplayState();
39
     }
41
     }
40
 
42
 
43
+    public void updateTabStyle(Bundle styleParams) {
44
+        StyleParams parsedStyleParams = new StyleParamsParser(styleParams).parse();
45
+        this.setStyleFromScreen(parsedStyleParams);
46
+    }
47
+
41
     public void setStyleFromScreen(StyleParams params) {
48
     public void setStyleFromScreen(StyleParams params) {
42
-        if (params.bottomTabsColor.hasColor()) {
43
-            setBackgroundColor(params.bottomTabsColor);
44
-        }
45
-        if (params.bottomTabsButtonColor.hasColor()) {
46
-            if (getInactiveColor() != params.bottomTabsButtonColor.getColor()) {
47
-                setInactiveColor(params.bottomTabsButtonColor.getColor());
48
-            }
49
-        }
50
-        if (params.selectedBottomTabsButtonColor.hasColor()) {
51
-            if (getAccentColor() != params.selectedBottomTabsButtonColor.getColor()) {
52
-                setAccentColor(params.selectedBottomTabsButtonColor.getColor());
53
-            }
54
-        }
49
+        this.setStyle(params);
55
 
50
 
56
         setVisibility(params.bottomTabsHidden, true);
51
         setVisibility(params.bottomTabsHidden, true);
57
     }
52
     }
132
                 Constants.BOTTOM_TABS_HEIGHT);
127
                 Constants.BOTTOM_TABS_HEIGHT);
133
     }
128
     }
134
 
129
 
135
-    private void setStyle() {
136
-        if (hasBadgeBackgroundColor()) {
130
+    private void setStyle(StyleParams params) {
131
+        if (params.bottomTabBadgeBackgroundColor.hasColor()) {
137
             setNotificationBackgroundColor(AppStyle.appStyle.bottomTabBadgeBackgroundColor.getColor());
132
             setNotificationBackgroundColor(AppStyle.appStyle.bottomTabBadgeBackgroundColor.getColor());
138
         }
133
         }
139
-        if (hasBadgeTextColor()) {
134
+        if (params.bottomTabBadgeTextColor.hasColor()) {
140
             setNotificationTextColor(AppStyle.appStyle.bottomTabBadgeTextColor.getColor());
135
             setNotificationTextColor(AppStyle.appStyle.bottomTabBadgeTextColor.getColor());
141
         }
136
         }
142
-    }
143
 
137
 
144
-    private boolean hasBadgeTextColor() {
145
-        return AppStyle.appStyle.bottomTabBadgeTextColor != null &&
146
-               AppStyle.appStyle.bottomTabBadgeTextColor.hasColor();
147
-    }
148
-
149
-    private boolean hasBadgeBackgroundColor() {
150
-        return AppStyle.appStyle.bottomTabBadgeBackgroundColor != null &&
151
-               AppStyle.appStyle.bottomTabBadgeBackgroundColor.hasColor();
138
+        if (params.bottomTabsColor.hasColor()) {
139
+            setBackgroundColor(params.bottomTabsColor);
140
+        }
141
+        if (params.bottomTabsButtonColor.hasColor()) {
142
+            if (getInactiveColor() != params.bottomTabsButtonColor.getColor()) {
143
+                setInactiveColor(params.bottomTabsButtonColor.getColor());
144
+            }
145
+        }
146
+        if (params.selectedBottomTabsButtonColor.hasColor()) {
147
+            if (getAccentColor() != params.selectedBottomTabsButtonColor.getColor()) {
148
+                setAccentColor(params.selectedBottomTabsButtonColor.getColor());
149
+            }
150
+        }
152
     }
151
     }
153
 
152
 
154
     private void setFontFamily() {
153
     private void setFontFamily() {

+ 17
- 17
ios/RCCNavigationController.m View File

337
   // setStyle
337
   // setStyle
338
   if ([performAction isEqualToString:@"setStyle"])
338
   if ([performAction isEqualToString:@"setStyle"])
339
   {
339
   {
340
-    
341
-    NSDictionary *navigatorStyle = actionParams;
342
-    
343
-    // merge the navigatorStyle of our parent
344
-    if ([self.topViewController isKindOfClass:[RCCViewController class]])
345
-    {
346
-      RCCViewController *parent = (RCCViewController*)self.topViewController;
347
-      NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:parent.navigatorStyle];
348
-      
349
-      // there are a few styles that we don't want to remember from our parent (they should be local)
350
-      [mergedStyle setValuesForKeysWithDictionary:navigatorStyle];
351
-      navigatorStyle = mergedStyle;
352
-      
353
-      parent.navigatorStyle = navigatorStyle;
354
-      
355
-      [parent setStyleOnInit];
356
-      [parent updateStyle];
340
+    for (UIViewController *viewController in self.viewControllers) {
341
+      if ([viewController isKindOfClass:[RCCViewController class]])
342
+      {
343
+        RCCViewController *rccViewController = (RCCViewController*)viewController;
344
+        
345
+        NSDictionary *navigatorStyle = [[NSDictionary alloc] initWithDictionary:actionParams copyItems:YES];
346
+        NSMutableDictionary *mergedStyle = [NSMutableDictionary dictionaryWithDictionary:rccViewController.navigatorStyle];
347
+        
348
+        // there are a few styles that we don't want to remember from our parent (they should be local)
349
+        [mergedStyle setValuesForKeysWithDictionary:navigatorStyle];
350
+        navigatorStyle = mergedStyle;
351
+        
352
+        rccViewController.navigatorStyle = navigatorStyle;
353
+        
354
+        [rccViewController setStyleOnInit];
355
+        [rccViewController updateStyle];
356
+      }
357
     }
357
     }
358
   }
358
   }
359
 }
359
 }

+ 1
- 0
ios/RCCTabBarController.h View File

5
 
5
 
6
 - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge;
6
 - (instancetype)initWithProps:(NSDictionary *)props children:(NSArray *)children globalProps:(NSDictionary*)globalProps bridge:(RCTBridge *)bridge;
7
 - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge completion:(void (^)(void))completion;
7
 - (void)performAction:(NSString*)performAction actionParams:(NSDictionary*)actionParams bridge:(RCTBridge *)bridge completion:(void (^)(void))completion;
8
++ (UIImage *)image:(UIImage*)image withColor:(UIColor *)color1;
8
 
9
 
9
 @end
10
 @end

+ 3
- 3
ios/RCCTabBarController.m View File

55
   return YES;
55
   return YES;
56
 }
56
 }
57
 
57
 
58
-- (UIImage *)image:(UIImage*)image withColor:(UIColor *)color1
58
++ (UIImage *)image:(UIImage*)image withColor:(UIColor *)color1
59
 {
59
 {
60
   UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
60
   UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
61
   CGContextRef context = UIGraphicsGetCurrentContext();
61
   CGContextRef context = UIGraphicsGetCurrentContext();
159
       iconImage = [RCTConvert UIImage:icon];
159
       iconImage = [RCTConvert UIImage:icon];
160
       if (buttonColor)
160
       if (buttonColor)
161
       {
161
       {
162
-        iconImage = [[self image:iconImage withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
162
+        iconImage = [[RCCTabBarController image:iconImage withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
163
       }
163
       }
164
     }
164
     }
165
     UIImage *iconImageSelected = nil;
165
     UIImage *iconImageSelected = nil;
327
       if (icon && icon != (id)[NSNull null])
327
       if (icon && icon != (id)[NSNull null])
328
       {
328
       {
329
         iconImage = [RCTConvert UIImage:icon];
329
         iconImage = [RCTConvert UIImage:icon];
330
-        iconImage = [[self image:iconImage withColor:self.tabBar.tintColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
330
+        iconImage = [[RCCTabBarController image:iconImage withColor:self.tabBar.tintColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
331
         viewController.tabBarItem.image = iconImage;
331
         viewController.tabBarItem.image = iconImage;
332
       }
332
       }
333
       
333
       

+ 88
- 4
ios/RCCViewController.m View File

484
     [viewController setNeedsStatusBarAppearanceUpdate];
484
     [viewController setNeedsStatusBarAppearanceUpdate];
485
   }
485
   }
486
   
486
   
487
-  NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
488
-  BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
489
-  if (tabBarHiddenBool) {
487
+  if (viewController.tabBarController && viewController.tabBarController.tabBar != (id)[NSNull null]) {
490
     UITabBar *tabBar = viewController.tabBarController.tabBar;
488
     UITabBar *tabBar = viewController.tabBarController.tabBar;
491
-    tabBar.transform = CGAffineTransformMakeTranslation(0, tabBar.frame.size.height);
489
+    
490
+    if (tabBar && tabBar != (id)[NSNull null]) {
491
+      UIColor *buttonColor = nil;
492
+      UIColor *selectedButtonColor = nil;
493
+      UIColor *labelColor = nil;
494
+      UIColor *selectedLabelColor = nil;
495
+      
496
+      NSNumber *tabBarHidden = self.navigatorStyle[@"tabBarHidden"];
497
+      BOOL tabBarHiddenBool = tabBarHidden ? [tabBarHidden boolValue] : NO;
498
+      if (tabBarHiddenBool) {
499
+        tabBar.transform = CGAffineTransformMakeTranslation(0, tabBar.frame.size.height);
500
+      }
501
+      
502
+      NSString *tabBarButtonColor = self.navigatorStyle[@"tabBarButtonColor"];
503
+      NSString *tabBarSelectedButtonColor = self.navigatorStyle[@"tabBarSelectedButtonColor"];
504
+      
505
+      if (tabBarButtonColor)
506
+      {
507
+        buttonColor = tabBarButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarButtonColor] : nil;
508
+        
509
+        if (tabBarSelectedButtonColor) {
510
+          selectedButtonColor = tabBarSelectedButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarSelectedButtonColor] : nil;
511
+          
512
+          tabBar.tintColor = selectedLabelColor = selectedButtonColor;
513
+          tabBar.unselectedItemTintColor = labelColor = buttonColor;
514
+        }
515
+        else {
516
+          tabBar.tintColor = labelColor = buttonColor;
517
+        }
518
+      }
519
+      else if (tabBarSelectedButtonColor) {
520
+        selectedButtonColor = tabBarSelectedButtonColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarSelectedButtonColor] : nil;
521
+        tabBar.tintColor = selectedLabelColor = selectedButtonColor;
522
+      }
523
+      
524
+      NSString *tabBarLabelColor = self.navigatorStyle[@"tabBarLabelColor"];
525
+      if(tabBarLabelColor) {
526
+        UIColor *color = tabBarLabelColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarLabelColor] : nil;
527
+        labelColor = color;
528
+      }
529
+      NSString *tabBarSelectedLabelColor = self.navigatorStyle[@"tabBarSelectedLabelColor"];
530
+      if(tabBarLabelColor) {
531
+        UIColor *color = tabBarSelectedLabelColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarSelectedLabelColor] : nil;
532
+        selectedLabelColor = color;
533
+      }
534
+      
535
+      NSString *tabBarBackgroundColor = self.navigatorStyle[@"tabBarBackgroundColor"];
536
+      if (tabBarBackgroundColor)
537
+      {
538
+        UIColor *color = tabBarBackgroundColor != (id)[NSNull null] ? [RCTConvert UIColor:tabBarBackgroundColor] : nil;
539
+        tabBar.barTintColor = color;
540
+      }
541
+      
542
+      NSNumber *tabBarTranslucent = self.navigatorStyle[@"tabBarTranslucent"];
543
+      if (tabBarTranslucent)
544
+      {
545
+        BOOL tabBarTranslucentBool = tabBarTranslucent ? [tabBarTranslucent boolValue] : NO;
546
+        tabBar.translucent = tabBarTranslucentBool;
547
+      }
548
+      
549
+      NSNumber *tabBarHideShadow = self.navigatorStyle[@"tabBarHideShadow"];
550
+      if (tabBarHideShadow)
551
+      {
552
+        BOOL tabBarHideShadowBool = tabBarHideShadow ? [tabBarHideShadow boolValue] : NO;
553
+        tabBar.clipsToBounds = tabBarHideShadowBool ? YES : NO;
554
+      }
555
+      
556
+      for (UIViewController *tabViewController in [viewController.tabBarController viewControllers]) {
557
+        NSMutableDictionary *unselectedAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"tabBarText" baseFont:[UIFont systemFontOfSize:10]];
558
+        if (!unselectedAttributes[NSForegroundColorAttributeName] && labelColor) {
559
+          unselectedAttributes[NSForegroundColorAttributeName] = labelColor;
560
+        }
561
+        [tabViewController.tabBarItem setTitleTextAttributes:unselectedAttributes forState:UIControlStateNormal];
562
+        
563
+        
564
+        NSMutableDictionary *selectedAttributes = [RCTHelpers textAttributesFromDictionary:self.navigatorStyle withPrefix:@"tabBarSelectedText" baseFont:[UIFont systemFontOfSize:10]];
565
+        if (!selectedAttributes[NSForegroundColorAttributeName] && selectedLabelColor) {
566
+          selectedAttributes[NSForegroundColorAttributeName] = selectedLabelColor;
567
+        }
568
+        [tabViewController.tabBarItem setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
569
+        
570
+        if (buttonColor)
571
+        {
572
+          tabViewController.tabBarItem.image = [[RCCTabBarController image:tabViewController.tabBarItem.image withColor:buttonColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
573
+        }
574
+      }
575
+    }
492
   }
576
   }
493
 
577
 
494
   NSNumber *navBarHidden = self.navigatorStyle[@"navBarHidden"];
578
   NSNumber *navBarHidden = self.navigatorStyle[@"navBarHidden"];