1/* 2 * Copyright (c) Meta Platforms, Inc. and affiliates. 3 * 4 * This source code is licensed under the MIT license found in the 5 * LICENSE file in the root directory of this source tree. 6 */ 7 8#import "ABI49_0_0RCTSegmentedControl.h" 9 10#import "ABI49_0_0RCTConvert.h" 11#import "ABI49_0_0UIView+React.h" 12 13@implementation ABI49_0_0RCTSegmentedControl 14 15- (instancetype)initWithFrame:(CGRect)frame 16{ 17 if ((self = [super initWithFrame:frame])) { 18 _selectedIndex = self.selectedSegmentIndex; 19 [self addTarget:self action:@selector(didChange) forControlEvents:UIControlEventValueChanged]; 20 } 21 return self; 22} 23 24- (void)setValues:(NSArray<NSString *> *)values 25{ 26 _values = [values copy]; 27 [self removeAllSegments]; 28 for (NSString *value in values) { 29 [self insertSegmentWithTitle:value atIndex:self.numberOfSegments animated:NO]; 30 } 31 super.selectedSegmentIndex = _selectedIndex; 32} 33 34- (void)setSelectedIndex:(NSInteger)selectedIndex 35{ 36 _selectedIndex = selectedIndex; 37 super.selectedSegmentIndex = selectedIndex; 38} 39 40- (void)setBackgroundColor:(UIColor *)backgroundColor 41{ 42#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ 43 __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 44 if (@available(iOS 13.0, *)) { 45 [super setBackgroundColor:backgroundColor]; 46 } 47#endif 48} 49 50- (void)setTextColor:(UIColor *)textColor 51{ 52#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ 53 __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 54 if (@available(iOS 13.0, *)) { 55 [self setTitleTextAttributes:@{NSForegroundColorAttributeName : textColor} forState:UIControlStateNormal]; 56 } 57#endif 58} 59 60- (void)setTintColor:(UIColor *)tintColor 61{ 62 [super setTintColor:tintColor]; 63#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ 64 __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 65 if (@available(iOS 13.0, *)) { 66 [self setSelectedSegmentTintColor:tintColor]; 67 [self setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} 68 forState:UIControlStateSelected]; 69 [self setTitleTextAttributes:@{NSForegroundColorAttributeName : tintColor} forState:UIControlStateNormal]; 70 } 71#endif 72} 73 74- (void)didChange 75{ 76 _selectedIndex = self.selectedSegmentIndex; 77 if (_onChange) { 78 _onChange(@{@"value" : [self titleForSegmentAtIndex:_selectedIndex], @"selectedSegmentIndex" : @(_selectedIndex)}); 79 } 80} 81 82@end 83