Skip to content

Commit c2b789d

Browse files
authored
Feat: add backgroundColor and textColor for iOS13 segmented contr… (#22)
Feat: add backgroundColor and textColor for iOS13 segmented control
2 parents b9f3da1 + 90ba080 commit c2b789d

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-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: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ - (instancetype)initWithFrame:(CGRect)frame
1818
if ((self = [super initWithFrame:frame])) {
1919
_selectedIndex = self.selectedSegmentIndex;
2020
[self addTarget:self action:@selector(didChange)
21-
forControlEvents:UIControlEventValueChanged];
21+
forControlEvents:UIControlEventValueChanged];
2222
}
2323
return self;
2424
}
@@ -39,6 +39,42 @@ - (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+
63+
- (void)setTintColor:(UIColor *)tintColor
64+
{
65+
[super setTintColor:tintColor];
66+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
67+
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
68+
if (@available(iOS 13.0, *)) {
69+
[self setSelectedSegmentTintColor:tintColor];
70+
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}
71+
forState:UIControlStateSelected];
72+
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: tintColor}
73+
forState:UIControlStateNormal];
74+
}
75+
#endif
76+
}
77+
4278
- (void)didChange
4379
{
4480
_selectedIndex = self.selectedSegmentIndex;

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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ type SegmentedControlIOSProps = $ReadOnly<{|
5151
* Accent color of the control.
5252
*/
5353
tintColor?: ?string,
54+
/**
55+
*
56+
* Text color of the control.
57+
* NOTE: this prop will only work for iOS >= 13
58+
*/
59+
textColor?: ?string,
60+
/**
61+
* Background color of the control.
62+
* NOTE: this prop will only work for iOS >= 13
63+
*/
64+
backgroundColor?: ?string,
65+
/**
5466
/**
5567
* If true, then selecting a segment won't persist visually.
5668
* The `onValueChange` callback will still work as expected.

0 commit comments

Comments
 (0)