Segmented control tintColor in iOS 6

2019-03-06 08:09发布

I have a segmented control with 8 segments. I can change the default tint-color of the whole control, BUT can I set a different color for each segment in the control? I found a tutorial that worked in 5.1 with a new class that calls this method,

-(void)setTintColor:(UIColor*)color forTag:(NSInteger)aTag{}

But it doesn't work in iOS 6. Any ideas?

5条回答
趁早两清
2楼-- · 2019-03-06 08:25

This issue has been fixed here. I could not paste the source code due to formatting issues. Sample code here.

EDIT: added comment & code from link and fixed formatting. ~olie

Its a hacky fix. This will work. Place your code in ViewDidAppear. That will do the trick.

- (void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear: animated];
    dispatch_async(dispatch_get_main_queue(), ^{
        for (int i = 0 ; i < [segmentControl.subviews count] ; i++)
        {
            if ([[segmentControl.subviews objectAtIndex: i] isSelected] )
            {
                [[segmentControl.subviews objectAtIndex: i] setTintColor: [UIColor blackColor]];
                break;
            }
        }
    }); 
}
查看更多
别忘想泡老子
3楼-- · 2019-03-06 08:30

You can set different segment image and color for each segment. For color you may use:

//get the subviews of the segmentedcontrol

NSArray *arri = [segmentedControl subviews];

//change the color of every subview(segment) you have

[[arri objectAtIndex:0] setTintColor:[UIColor redColor]];

[[arri objectAtIndex:1] setTintColor:[UIColor greenColor]];

Hope that solves the problem.

查看更多
乱世女痞
4楼-- · 2019-03-06 08:31

You are right... iOS 6 doesn't support subviews for segmented control....

I have an alternative for you:

CGRect rect = CGRectMake(0, 0, 80, 44);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
                               [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[segment setImage:img forSegmentAtIndex:0];

You need to have core graphics framework added to the project.

We can draw an image for segment at index.... But if you use this, you won't be able to add text using segment title. You will need to draw text also over the image 'img' used above. Please share if you get any other way of doing it.

查看更多
可以哭但决不认输i
5楼-- · 2019-03-06 08:33

UiSegmentedControl has a property 'segmentedControlStyle' (deprecated in iOS7) that affect the behavior of 'tintColor'

the possible styles are:

UISegmentedControlStylePlain,   
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar,     
UISegmentedControlStyleBezeled, 

but actually in iOS6 'Bezeled' (deprecated) is equal to 'Bar'

with the first two styles there is no way to change have applied the 'tintColor', to customize it you need to change the images for each segment using:

- (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment;

in this way you will obtain a completely custom segmented control

But if the defaul is enough for your design you can just use the style

UISegmentedControlStyleBar

and the 'tintColor' property will take effect and you will obtain a colored segmented control applying the tint depending to the selected segment and all the other benefits letting the system dial with it.

查看更多
来,给爷笑一个
6楼-- · 2019-03-06 08:36

Here is an easy solution setting a red color and compatible with iOS 6.

for ( UIView *segmentView in [segmentedControl subviews] ) {
    if ( [segmentView respondsToSelector:@selector(setTintColor:)] ) {
        [segmentView performSelector:@selector(setTintColor:)
                          withObject:[UIColor redColor]];
    }
}
查看更多
登录 后发表回答