-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathRNCSegmentedControl.m
More file actions
110 lines (96 loc) · 3.24 KB
/
Copy pathRNCSegmentedControl.m
File metadata and controls
110 lines (96 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RNCSegmentedControl.h"
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <React/UIView+React.h>
@implementation RNCSegmentedControl
- (instancetype)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
_selectedIndex = self.selectedSegmentIndex;
[self addTarget:self
action:@selector(didChange)
forControlEvents:UIControlEventValueChanged];
}
return self;
}
- (void)setValues:(NSArray *)values {
[self removeAllSegments];
for (id segment in values) {
if ([segment isKindOfClass:[NSMutableDictionary class]]){
UIImage *image = [[RCTConvert UIImage:segment] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self insertSegmentWithImage:image
atIndex:self.numberOfSegments
animated:NO];
} else {
[self insertSegmentWithTitle:(NSString *)segment
atIndex:self.numberOfSegments
animated:NO];
}
}
super.selectedSegmentIndex = _selectedIndex;
}
- (void)setSelectedIndex:(NSInteger)selectedIndex {
_selectedIndex = 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)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];
}
#endif
}
- (void)didChange {
_selectedIndex = self.selectedSegmentIndex;
if (_onChange) {
NSString *segmentTitle = [self titleForSegmentAtIndex:_selectedIndex];
_onChange(@{
@"value" : (segmentTitle) ? segmentTitle : [self imageForSegmentAtIndex:_selectedIndex],
@"selectedSegmentIndex" : @(_selectedIndex)
});
}
}
- (void)setAppearance:(NSString *)appearanceString {
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
if ([appearanceString isEqual:@"dark"]) {
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark];
} else if ([appearanceString isEqual:@"light"]) {
[self setOverrideUserInterfaceStyle:UIUserInterfaceStyleLight];
}
}
#endif
}
- (NSArray *)accessibilityElements {
NSArray *elements = [super accessibilityElements];
[elements enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
@try {
obj.accessibilityIdentifier = self.testIDS[idx];
} @catch (NSException *exception) {
NSLog(@"%@", exception);
}
}];
return elements;
}
// Prevents the segmented control from interfering with scroll gestures in ScrollViews.
// See https://stackoverflow.com/a/58193949
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return YES;
}
@end