Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,23 @@ export default class App extends React.Component<{}, $FlowFixMeState> {
tintColor="#ff0000"
values={['One', 'Two', 'Three', 'Four']}
selectedIndex={0}
backgroundColor="#0000ff"
/>
</View>
<View style={{marginBottom: 25}}>
<View style={{marginBottom: 10}}>
<SegmentedControlIOS
tintColor="#00ff00"
values={['One', 'Two', 'Three']}
selectedIndex={1}
/>
</View>
<View style={{marginBottom: 25}}>
<SegmentedControlIOS
textColor="#ff00ff"
values={['One', 'Two']}
selectedIndex={1}
/>
</View>

<View>
<Text style={styles.text}>Custom colors can be provided</Text>
Expand Down
38 changes: 37 additions & 1 deletion ios/RCTSegmentedControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ - (instancetype)initWithFrame:(CGRect)frame
if ((self = [super initWithFrame:frame])) {
_selectedIndex = self.selectedSegmentIndex;
[self addTarget:self action:@selector(didChange)
forControlEvents:UIControlEventValueChanged];
forControlEvents:UIControlEventValueChanged];
}
return self;
}
Expand All @@ -39,6 +39,42 @@ - (void)setSelectedIndex:(NSInteger)selectedIndex
super.selectedSegmentIndex = selectedIndex;
}

- (void)setBackgroundColor:(UIColor *)backgroundColor
{
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
[super setBackgroundColor:backgroundColor];
}
#endif
}

- (void)setTextColor:(UIColor *)textColor
{
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: textColor}
forState:UIControlStateNormal];
}
#endif
}

- (void)setTintColor:(UIColor *)tintColor
{
[super setTintColor:tintColor];
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
[self setSelectedSegmentTintColor:tintColor];
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}
forState:UIControlStateSelected];
[self setTitleTextAttributes:@{NSForegroundColorAttributeName: tintColor}
forState:UIControlStateNormal];
}
#endif
}

- (void)didChange
{
_selectedIndex = self.selectedSegmentIndex;
Expand Down
2 changes: 2 additions & 0 deletions ios/RCTSegmentedControlManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(values, NSArray<NSString *>)
RCT_EXPORT_VIEW_PROPERTY(selectedIndex, NSInteger)
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(backgroundColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(textColor, UIColor)
RCT_EXPORT_VIEW_PROPERTY(momentary, BOOL)
RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
Expand Down
12 changes: 12 additions & 0 deletions js/RCTSegmentedControlNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ type SegmentedControlIOSProps = $ReadOnly<{|
* Accent color of the control.
*/
tintColor?: ?string,
/**
*
* Text color of the control.
* NOTE: this prop will only work for iOS >= 13
*/
textColor?: ?string,
/**
* Background color of the control.
* NOTE: this prop will only work for iOS >= 13
*/
backgroundColor?: ?string,
/**
/**
* If true, then selecting a segment won't persist visually.
* The `onValueChange` callback will still work as expected.
Expand Down