From 14510cfefe795f03a6d6073eb3ba86e22676010e Mon Sep 17 00:00:00 2001 From: Jesse Katsumata Date: Tue, 31 Dec 2019 11:11:41 +0900 Subject: [PATCH 1/3] fix: tintColor for iOS 13 --- ios/RCTSegmentedControl.m | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ios/RCTSegmentedControl.m b/ios/RCTSegmentedControl.m index adbde608..0b4a9ebe 100644 --- a/ios/RCTSegmentedControl.m +++ b/ios/RCTSegmentedControl.m @@ -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; } @@ -39,6 +39,21 @@ - (void)setSelectedIndex:(NSInteger)selectedIndex super.selectedSegmentIndex = selectedIndex; } +- (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; From a97ae2cc9a7a690f337bcbab880ba3d539f80f73 Mon Sep 17 00:00:00 2001 From: Jesse Katsumata Date: Tue, 31 Dec 2019 12:11:15 +0900 Subject: [PATCH 2/3] feat: add backgroundColor and Text Color --- example/App.js | 10 +++++++++- ios/RCTSegmentedControl.m | 23 ++++++++++++++++++++++- ios/RCTSegmentedControlManager.m | 2 ++ js/RCTSegmentedControlNativeComponent.js | 10 ++++++++++ 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/example/App.js b/example/App.js index 8b594d29..77746f9d 100644 --- a/example/App.js +++ b/example/App.js @@ -60,15 +60,23 @@ export default class App extends React.Component<{}, $FlowFixMeState> { tintColor="#ff0000" values={['One', 'Two', 'Three', 'Four']} selectedIndex={0} + backgroundColor="#0000ff" /> - + + + + Custom colors can be provided diff --git a/ios/RCTSegmentedControl.m b/ios/RCTSegmentedControl.m index 0b4a9ebe..cead275b 100644 --- a/ios/RCTSegmentedControl.m +++ b/ios/RCTSegmentedControl.m @@ -39,6 +39,27 @@ - (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]; @@ -49,7 +70,7 @@ - (void)setTintColor:(UIColor *)tintColor [self setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateSelected]; [self setTitleTextAttributes:@{NSForegroundColorAttributeName: tintColor} - forState:UIControlStateNormal]; + forState:UIControlStateNormal]; } #endif } diff --git a/ios/RCTSegmentedControlManager.m b/ios/RCTSegmentedControlManager.m index 2994065d..75edcc78 100644 --- a/ios/RCTSegmentedControlManager.m +++ b/ios/RCTSegmentedControlManager.m @@ -23,6 +23,8 @@ - (UIView *)view RCT_EXPORT_VIEW_PROPERTY(values, NSArray) 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) diff --git a/js/RCTSegmentedControlNativeComponent.js b/js/RCTSegmentedControlNativeComponent.js index 949f54f7..df1eb83d 100644 --- a/js/RCTSegmentedControlNativeComponent.js +++ b/js/RCTSegmentedControlNativeComponent.js @@ -51,6 +51,16 @@ type SegmentedControlIOSProps = $ReadOnly<{| * Accent color of the control. */ tintColor?: ?string, + /** + * + * Text color of the control. + */ + textColor?: ?string, + /** + * Background color of the control. + */ + backgroundColor?: ?string, + /** /** * If true, then selecting a segment won't persist visually. * The `onValueChange` callback will still work as expected. From 90ba08041eedcd2f69ada648c40e521215eacdfb Mon Sep 17 00:00:00 2001 From: Jesse Katsumata Date: Tue, 31 Dec 2019 12:14:31 +0900 Subject: [PATCH 3/3] chore: add note for added types --- js/RCTSegmentedControlNativeComponent.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/js/RCTSegmentedControlNativeComponent.js b/js/RCTSegmentedControlNativeComponent.js index df1eb83d..5f86b3b9 100644 --- a/js/RCTSegmentedControlNativeComponent.js +++ b/js/RCTSegmentedControlNativeComponent.js @@ -54,10 +54,12 @@ type SegmentedControlIOSProps = $ReadOnly<{| /** * * 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, /**