Skip to content

Commit a97ae2c

Browse files
committed
feat: add backgroundColor and Text Color
1 parent 14510cf commit a97ae2c

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

example/App.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,23 @@ export default class App extends React.Component<{}, $FlowFixMeState> {
6060
tintColor="#ff0000"
6161
values={['One', 'Two', 'Three', 'Four']}
6262
selectedIndex={0}
63+
backgroundColor="#0000ff"
6364
/>
6465
</View>
65-
<View style={{marginBottom: 25}}>
66+
<View style={{marginBottom: 10}}>
6667
<SegmentedControlIOS
6768
tintColor="#00ff00"
6869
values={['One', 'Two', 'Three']}
6970
selectedIndex={1}
7071
/>
7172
</View>
73+
<View style={{marginBottom: 25}}>
74+
<SegmentedControlIOS
75+
textColor="#ff00ff"
76+
values={['One', 'Two']}
77+
selectedIndex={1}
78+
/>
79+
</View>
7280

7381
<View>
7482
<Text style={styles.text}>Custom colors can be provided</Text>

ios/RCTSegmentedControl.m

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ - (void)setSelectedIndex:(NSInteger)selectedIndex
3939
super.selectedSegmentIndex = selectedIndex;
4040
}
4141

42+
- (void)setBackgroundColor:(UIColor *)backgroundColor
43+
{
44+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
45+
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
46+
if (@available(iOS 13.0, *)) {
47+
[super setBackgroundColor:backgroundColor];
48+
}
49+
#endif
50+
}
51+
52+
- (void)setTextColor:(UIColor *)textColor
53+
{
54+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
55+
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
56+
if (@available(iOS 13.0, *)) {
57+
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: textColor}
58+
forState:UIControlStateNormal];
59+
}
60+
#endif
61+
}
62+
4263
- (void)setTintColor:(UIColor *)tintColor
4364
{
4465
[super setTintColor:tintColor];
@@ -49,7 +70,7 @@ - (void)setTintColor:(UIColor *)tintColor
4970
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}
5071
forState:UIControlStateSelected];
5172
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: tintColor}
52-
forState:UIControlStateNormal];
73+
forState:UIControlStateNormal];
5374
}
5475
#endif
5576
}

ios/RCTSegmentedControlManager.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ - (UIView *)view
2323
RCT_EXPORT_VIEW_PROPERTY(values, NSArray<NSString *>)
2424
RCT_EXPORT_VIEW_PROPERTY(selectedIndex, NSInteger)
2525
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)
26+
RCT_EXPORT_VIEW_PROPERTY(backgroundColor, UIColor)
27+
RCT_EXPORT_VIEW_PROPERTY(textColor, UIColor)
2628
RCT_EXPORT_VIEW_PROPERTY(momentary, BOOL)
2729
RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL)
2830
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)

js/RCTSegmentedControlNativeComponent.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ type SegmentedControlIOSProps = $ReadOnly<{|
5151
* Accent color of the control.
5252
*/
5353
tintColor?: ?string,
54+
/**
55+
*
56+
* Text color of the control.
57+
*/
58+
textColor?: ?string,
59+
/**
60+
* Background color of the control.
61+
*/
62+
backgroundColor?: ?string,
63+
/**
5464
/**
5565
* If true, then selecting a segment won't persist visually.
5666
* The `onValueChange` callback will still work as expected.

0 commit comments

Comments
 (0)