自定义UISegmentedControl,添加背景图像和选定的段色调颜色

这个的共同点,但它不适合我。

我使用UICatalog创建了UISegmentedControl,并尝试更改选定的段颜色。 我用它来改变颜色。 背景图像工作正常但不改变选定的段颜色。 我应该做些什么修改? 或者任何其他方法? 我的代码如下。

NSArray *segmentTextContent = @[@"First",@"Second",@"Third"]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent]; segmentedControl.frame = CGRectMake(20, 50, 280, 30); [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; segmentedControl.selectedSegmentIndex = 1; segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; [segmentedControl setBackgroundImage:[UIImage imageNamed:@"navigationBar"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; [segmentedControl setDividerImage:[UIImage imageNamed:@"divider"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; // we want attributed strings for this segmented control NSDictionary *textAttributes = @{ UITextAttributeTextColor:[UIColor whiteColor], UITextAttributeFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13] }; [segmentedControl setTitleTextAttributes:textAttributes forState:UIControlStateNormal]; textAttributes = @{ UITextAttributeTextColor:[UIColor whiteColor], UITextAttributeFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13] }; [segmentedControl setTitleTextAttributes:textAttributes forState:UIControlStateHighlighted]; [self.view addSubview:segmentedControl]; - (void)segmentAction:(UISegmentedControl *)sender { for (int i=0; i<[sender.subviews count]; i++) { if ([[sender.subviews objectAtIndex:i]isSelected]) { UIColor *tintcolor = [UIColor greenColor]; [[sender.subviews objectAtIndex:i] setTintColor:tintcolor]; } else { [[sender.subviews objectAtIndex:i] setTintColor:nil]; } } } 

使用setBackgroundImage:forState:barMetrics:使用UIControlStateSelected作为状态。

在iOS 7中使用tintColor的新行为,请尝试设置背景的颜色。 这将在选择时更改segmentedControl的文本颜色。
在将segmentedControl添加到视图之前添加此行:

 segmentedControl.backgroundColor = [UIColor greenColor]; 

所以你不再需要它了:

 - (void)segmentAction:(UISegmentedControl *)sender { for (int i=0; i<[sender.subviews count]; i++) { if ([[sender.subviews objectAtIndex:i]isSelected]) { UIColor *tintcolor = [UIColor greenColor]; [[sender.subviews objectAtIndex:i] setTintColor:tintcolor]; } else { [[sender.subviews objectAtIndex:i] setTintColor:nil]; } } } 

请记住,未选定的segmentedControl的背景颜色也会发生变化。 但是如果你有自定义图像,你就不会看到它。

希望有所帮助。

对于UISegmentedControl,您可以使用此代码

 for (int i=0; i<[sender.subviews count]; i++) { if ([[sender.subviews objectAtIndex:i]isSelected] ) { UIColor *tintcolor=[UIColor colorWithRed:127.0/255.0 green:161.0/255.0 blue:183.0/255.0 alpha:1.0]; [[sender.subviews objectAtIndex:i] setTintColor:tintcolor]; } else { [[sender.subviews objectAtIndex:i] setTintColor:nil]; } } 

对于背景图像

 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible];