@@ -26,8 +26,8 @@ - (void)updateBackgrounds;
26
26
@interface URBSegmentedControl ()
27
27
@property (nonatomic , strong ) NSMutableArray *items;
28
28
@property (nonatomic , strong ) UIImageView *backgroundView;
29
- @property (nonatomic ) UIEdgeInsets segmentEdgeInsets;
30
29
@property (nonatomic , strong ) NSDictionary *segmentTextAttributes;
30
+ @property (nonatomic , strong ) NSDictionary *segmentDisabledTextAttributes;
31
31
- (void )layoutSegments ;
32
32
- (void )handleSelect : (URBSegmentView *)segmentView ;
33
33
- (NSInteger )firstSegmentIndexNearIndex : (NSUInteger )index enabled : (BOOL )enabled ;
@@ -55,11 +55,12 @@ - (void)initInternal{
55
55
self.segmentEdgeInsets = UIEdgeInsetsMake (4 .0f , 4 .0f , 4 .0f , 4 .0f );
56
56
57
57
// base styles
58
- self.baseColor = [UIColor colorWithRed :0.35 green: 0.35 blue: 0.35 alpha: 1.0 ];
58
+ self.baseColor = [UIColor colorWithWhite :0.3 alpha: 1.0 ];
59
59
self.strokeColor = [UIColor darkGrayColor ];
60
60
self.segmentBackgroundColor = nil ;
61
61
self.strokeWidth = 2 .0f ;
62
62
self.cornerRadius = 4 .0f ;
63
+ self.showsGradient = YES ;
63
64
64
65
// layout
65
66
self.layoutOrientation = URBSegmentedControlOrientationHorizontal;
@@ -116,16 +117,15 @@ - (id)initWithTitles:(NSArray *)titles icons:(NSArray *)icons {
116
117
return self;
117
118
}
118
119
119
- /* * Initialize from nib file */
120
120
- (id )initWithCoder : (NSCoder *)aDecoder {
121
121
self = [super initWithCoder: aDecoder];
122
- if (self){
122
+ if (self) {
123
123
CGRect nibFrame = self.frame ;
124
124
[self initInternal ];
125
125
126
- // Restore nib settings
126
+ // restore nib settings
127
127
self.frame = nibFrame;
128
- for (NSInteger i= 0 ; i< super.numberOfSegments ; i++){
128
+ for (NSInteger i = 0 ; i < super.numberOfSegments ; i++){
129
129
[self insertSegmentWithTitle: [super titleForSegmentAtIndex: i] atIndex: i animated: NO ];
130
130
}
131
131
}
@@ -153,9 +153,13 @@ - (void)insertSegmentWithTitle:(NSString *)title image:(UIImage *)image atIndex:
153
153
154
154
// style the segment
155
155
segmentView.viewLayout = self.segmentViewLayout ;
156
+ segmentView.showsGradient = self.showsGradient ;
156
157
if (self.segmentTextAttributes ) {
157
158
[segmentView setTextAttributes: self .segmentTextAttributes forState: UIControlStateNormal];
158
159
}
160
+ if (self.segmentDisabledTextAttributes ) {
161
+ [segmentView setTextAttributes: self .segmentDisabledTextAttributes forState: UIControlStateDisabled];
162
+ }
159
163
160
164
// set custom styles if defined
161
165
segmentView.cornerRadius = self.cornerRadius ;
@@ -322,7 +326,7 @@ - (BOOL)isEnabledForSegmentAtIndex:(NSUInteger)segment {
322
326
323
327
- (void )setSelectedSegmentIndex : (NSInteger )selectedSegmentIndex {
324
328
if (_selectedSegmentIndex != selectedSegmentIndex) {
325
- NSParameterAssert (selectedSegmentIndex < (NSInteger )self.items.count);
329
+ NSParameterAssert (selectedSegmentIndex < (NSInteger )self.items.count && selectedSegmentIndex >= 0 );
326
330
327
331
// deselect current segment if selected
328
332
if (_selectedSegmentIndex >= 0 )
@@ -365,7 +369,14 @@ - (CGFloat)widthForSegmentAtIndex:(NSUInteger)segment {
365
369
#pragma mark - Customization
366
370
367
371
- (void )setTextAttributes : (NSDictionary *)textAttributes forState : (UIControlState)state {
368
- self.segmentTextAttributes = textAttributes;
372
+ if (state == UIControlStateDisabled)
373
+ self.segmentDisabledTextAttributes = textAttributes;
374
+ else
375
+ self.segmentTextAttributes = textAttributes;
376
+
377
+ [self .items enumerateObjectsUsingBlock: ^(URBSegmentView *segmentView, NSUInteger idx, BOOL *stop) {
378
+ [segmentView setTextAttributes: textAttributes forState: state];
379
+ }];
369
380
}
370
381
371
382
- (void )setSegmentBackgroundColor : (UIColor *)segmentBackgroundColor {
@@ -401,10 +412,10 @@ - (UIImage *)defaultBackgroundImage {
401
412
402
413
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
403
414
CGContextRef context = UIGraphicsGetCurrentContext ();
404
-
415
+
405
416
// colors
406
- UIColor* baseGradientTopColor = [self .baseColor adjustBrightness: 1.1 ];
407
- UIColor* baseGradientBottomColor = [self .baseColor adjustBrightness: 0.9 ];
417
+ UIColor* baseGradientTopColor = (self. showsGradient ) ? [self .baseColor adjustBrightness: 1.1 ] : self. baseColor ;
418
+ UIColor* baseGradientBottomColor = (self. showsGradient ) ? [self .baseColor adjustBrightness: 0.9 ] : self. baseColor ;
408
419
UIColor* baseStrokeColor = self.strokeColor ;
409
420
410
421
// gradients
0 commit comments