1/** 2 * Copyright (c) Facebook, Inc. and its 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 "RNCSegmentedControl.h" 9 10#import <React/RCTConvert.h> 11#import <React/RCTEventDispatcher.h> 12#import <React/UIView+React.h> 13 14@implementation RNCSegmentedControl 15 16- (instancetype)initWithFrame:(CGRect)frame { 17 if ((self = [super initWithFrame:frame])) { 18 _selectedIndex = self.selectedSegmentIndex; 19 [self addTarget:self 20 action:@selector(didChange) 21 forControlEvents:UIControlEventValueChanged]; 22 } 23 return self; 24} 25 26- (void)setValues:(NSArray *)values { 27 [self removeAllSegments]; 28 for (id segment in values) { 29 if ([segment isKindOfClass:[NSMutableDictionary class]]){ 30 UIImage *image = [[RCTConvert UIImage:segment] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 31 [self insertSegmentWithImage:image 32 atIndex:self.numberOfSegments 33 animated:NO]; 34 } else { 35 [self insertSegmentWithTitle:(NSString *)segment 36 atIndex:self.numberOfSegments 37 animated:NO]; 38 } 39 } 40 super.selectedSegmentIndex = _selectedIndex; 41} 42 43- (void)setSelectedIndex:(NSInteger)selectedIndex { 44 _selectedIndex = selectedIndex; 45 super.selectedSegmentIndex = selectedIndex; 46} 47 48- (void)setBackgroundColor:(UIColor *)backgroundColor { 49#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ 50 __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 51 if (@available(iOS 13.0, *)) { 52 [super setBackgroundColor:backgroundColor]; 53 } 54#endif 55} 56 57- (void)setTintColor:(UIColor *)tintColor { 58 [super setTintColor:tintColor]; 59#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ 60 __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 61 if (@available(iOS 13.0, *)) { 62 [self setSelectedSegmentTintColor:tintColor]; 63 NSDictionary *attributes = [NSDictionary 64 dictionaryWithObjectsAndKeys:tintColor, NSForegroundColorAttributeName, 65 nil]; 66 NSDictionary *activeAttributes = [NSDictionary 67 dictionaryWithObjectsAndKeys:UIColor.labelColor, 68 NSForegroundColorAttributeName, nil]; 69 [self setTitleTextAttributes:attributes forState:UIControlStateNormal]; 70 [self setTitleTextAttributes:activeAttributes 71 forState:UIControlStateSelected]; 72 } 73#endif 74} 75 76- (void)didChange { 77 _selectedIndex = self.selectedSegmentIndex; 78 if (_onChange) { 79 NSString *segmentTitle = [self titleForSegmentAtIndex:_selectedIndex]; 80 _onChange(@{ 81 @"value" : (segmentTitle) ? segmentTitle : [self imageForSegmentAtIndex:_selectedIndex], 82 @"selectedSegmentIndex" : @(_selectedIndex) 83 }); 84 } 85} 86 87- (void)setAppearance:(NSString *)appearanceString { 88#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ 89 __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 90 if (@available(iOS 13.0, *)) { 91 if ([appearanceString isEqual:@"dark"]) { 92 [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark]; 93 } else if ([appearanceString isEqual:@"light"]) { 94 [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleLight]; 95 } 96 } 97#endif 98} 99 100@end 101