UISegmented Control - setting the tint color of each segment

白昼怎懂夜的黑 提交于 2020-01-13 19:08:11

问题


I have been wanting to apply different colors to my UISegmentedControl segments. Many people on here have been asking how to set tint color when you press on a certain segment. What i want to do is to set the tint color of each segment throughout the life of the application(or when the view appears on the screen).

Scanning the questions on here, different people have pointed out that evidently in iOS 6 you cannot set the tintColor of each segment as such:

- (void)viewDidLoad
{
    [super viewDidLoad];
    buttonNames = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", nil];
    colorControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
    [[[colorControl subviews] objectAtIndex:0] setTintColor:[UIColor redColor]];
    [[[colorControl subviews] objectAtIndex:1] setTintColor:[UIColor greenColor]];
    [[[colorControl subviews] objectAtIndex:2] setTintColor:[UIColor blueColor]];
    colorControl.segmentedControlStyle = UISegmentedControlStyleBar;
    colorControl.momentary = YES;

    [colorControl addTarget:self action:@selector(colorSegmentSelected:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:colorControl];

}

I went and installed the iOS 5.0 and 5.1 simulators within XCode and set my project to target 5.0 and 5.1. In both cases this code would still not work. The tintColor of all 3 segments were gray.

  1. what is the proper way to set the tint color of each segment(if i have done it wrong here please correct me.
  2. If this code did work in iOS 5 how come that even when i target 5.0/5.1 and use the appropriate simulators it still doesnt work?

appreciate the feedback/knowledge on this subject. thanks!

来源:https://stackoverflow.com/questions/13827297/uisegmented-control-setting-the-tint-color-of-each-segment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!