Skip to content

Commit e8f577e

Browse files
Naturalclarfacebook-github-bot
authored andcommitted
feat: add custom color for iOS13 segmented control (#27643)
Summary: Addresses Issue from react-native-segmented-control/segmented-control#16 SegmentedControlIOS changed how it looks in iOS13. This PR allows allows more customization of SegmentedControl for iOS13. ## Changelog [iOS] [Added] - add textColor and backgroundColor props for iOS >=13 Pull Request resolved: #27643 Test Plan: | Before | After | | --- | --- | | <img src="https://user-images.githubusercontent.com/6936373/71608475-e68ff580-2bc4-11ea-9fe4-b85b99130356.png" width="320" /> | <img src="https://user-images.githubusercontent.com/6936373/71608757-dc6ef680-2bc6-11ea-85be-aa31f25ecf36.png" width="320" /> | Differential Revision: D19296783 Pulled By: cpojer fbshipit-source-id: 81a31b2d5ae3085a6fd1874e7d72e75be4c51318
1 parent ff9def4 commit e8f577e

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

Libraries/Components/SegmentedControlIOS/RCTSegmentedControlNativeComponent.js

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type NativeProps = $ReadOnly<{|
3333
selectedIndex?: WithDefault<Int32, 0>,
3434
enabled?: WithDefault<boolean, true>,
3535
tintColor?: ?ColorValue,
36+
textColor?: ?ColorValue,
37+
backgroundColor?: ?ColorValue,
3638
momentary?: WithDefault<boolean, false>,
3739

3840
// Events

React/Views/RCTSegmentedControl.m

+21
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];

React/Views/RCTSegmentedControlManager.m

+2
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)

ReactAndroid/src/main/java/com/facebook/react/viewmanagers/SegmentedControlManagerDelegate.java

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public void setProperty(T view, String propName, @Nullable Object value) {
3535
case "tintColor":
3636
mViewManager.setTintColor(view, value == null ? null : ((Double) value).intValue());
3737
break;
38+
case "textColor":
39+
mViewManager.setTextColor(view, value == null ? null : ((Double) value).intValue());
40+
break;
41+
case "backgroundColor":
42+
mViewManager.setBackgroundColor(view, value == null ? null : ((Double) value).intValue());
43+
break;
3844
case "momentary":
3945
mViewManager.setMomentary(view, value == null ? false : (boolean) value);
4046
break;

ReactAndroid/src/main/java/com/facebook/react/viewmanagers/SegmentedControlManagerInterface.java

+2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ public interface SegmentedControlManagerInterface<T extends View> {
1818
void setSelectedIndex(T view, int value);
1919
void setEnabled(T view, boolean value);
2020
void setTintColor(T view, @Nullable Integer value);
21+
void setTextColor(T view, @Nullable Integer value);
22+
void setBackgroundColor(T view, @Nullable Integer value);
2123
void setMomentary(T view, boolean value);
2224
}

0 commit comments

Comments
 (0)