Browse Source

pass imageInsets for tab icon [iOS] (#1138)

Pass iconInsets for tab icon [iOS]
Gevorg Gasparyan 7 years ago
parent
commit
84f4462802
2 changed files with 23 additions and 1 deletions
  1. 7
    1
      docs/top-level-api.md
  2. 16
    0
      ios/RCCTabBarController.m

+ 7
- 1
docs/top-level-api.md View File

30
       screen: 'example.FirstTabScreen', // unique ID registered with Navigation.registerScreen
30
       screen: 'example.FirstTabScreen', // unique ID registered with Navigation.registerScreen
31
       icon: require('../img/one.png'), // local image asset for the tab icon unselected state (optional on iOS)
31
       icon: require('../img/one.png'), // local image asset for the tab icon unselected state (optional on iOS)
32
       selectedIcon: require('../img/one_selected.png'), // local image asset for the tab icon selected state (optional, iOS only. On Android, Use `tabBarSelectedButtonColor` instead)
32
       selectedIcon: require('../img/one_selected.png'), // local image asset for the tab icon selected state (optional, iOS only. On Android, Use `tabBarSelectedButtonColor` instead)
33
+      iconInsets: { // add this to change icon position (optional, iOS only).
34
+        top: 6, // optional, default is 0.
35
+        left: 0, // optional, default is 0.
36
+        bottom: -6, // optional, default is 0.
37
+        right: 0 // optional, default is 0.
38
+      },
33
       title: 'Screen One', // title of the screen as appears in the nav bar (optional)
39
       title: 'Screen One', // title of the screen as appears in the nav bar (optional)
34
       navigatorStyle: {}, // override the navigator style for the tab screen, see "Styling the navigator" below (optional),
40
       navigatorStyle: {}, // override the navigator style for the tab screen, see "Styling the navigator" below (optional),
35
       navigatorButtons: {} // override the nav buttons for the tab screen, see "Adding buttons to the navigator" below (optional)
41
       navigatorButtons: {} // override the nav buttons for the tab screen, see "Adding buttons to the navigator" below (optional)
157
 
163
 
158
 ```js
164
 ```js
159
 Navigation.registerScreen('example.AdvancedScreen', () => AdvancedScreen);
165
 Navigation.registerScreen('example.AdvancedScreen', () => AdvancedScreen);
160
-```
166
+```

+ 16
- 0
ios/RCCTabBarController.m View File

146
     viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
146
     viewController.tabBarItem.accessibilityIdentifier = tabItemLayout[@"props"][@"testID"];
147
     viewController.tabBarItem.selectedImage = iconImageSelected;
147
     viewController.tabBarItem.selectedImage = iconImageSelected;
148
     
148
     
149
+    id imageInsets = tabItemLayout[@"props"][@"iconInsets"];
150
+    if (imageInsets && imageInsets != (id)[NSNull null])
151
+    {
152
+      id topInset = imageInsets[@"top"];
153
+      id leftInset = imageInsets[@"left"];
154
+      id bottomInset = imageInsets[@"bottom"];
155
+      id rightInset = imageInsets[@"right"];
156
+      
157
+      CGFloat top = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:topInset] : 0;
158
+      CGFloat left = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:leftInset] : 0;
159
+      CGFloat bottom = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:bottomInset] : 0;
160
+      CGFloat right = topInset != (id)[NSNull null] ? [RCTConvert CGFloat:rightInset] : 0;
161
+      
162
+      viewController.tabBarItem.imageInsets = UIEdgeInsetsMake(top, left, bottom, right);
163
+    }
164
+    
149
     NSMutableDictionary *unselectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarText" baseFont:[UIFont systemFontOfSize:10]];
165
     NSMutableDictionary *unselectedAttributes = [RCTHelpers textAttributesFromDictionary:tabsStyle withPrefix:@"tabBarText" baseFont:[UIFont systemFontOfSize:10]];
150
     if (!unselectedAttributes[NSForegroundColorAttributeName] && buttonColor) {
166
     if (!unselectedAttributes[NSForegroundColorAttributeName] && buttonColor) {
151
       unselectedAttributes[NSForegroundColorAttributeName] = buttonColor;
167
       unselectedAttributes[NSForegroundColorAttributeName] = buttonColor;