iphone ios7分段UISegmentedControl只更改边框颜色

四处张望,试图改变只是边界颜色(用不同的文字颜色),没有运气。 可以改变色调,但改变文字和边框。

您可以使用UIAppearance代理设置标题文本属性,但保留tintColor边框。 就像是:

[[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateNormal]; 

编辑:

为了给图像着色,你可以在UImage中使用类似的东西:

 - (instancetype)tintedImageWithColor:(UIColor *)tintColor { UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0); CGContextRef context = UIGraphicsGetCurrentContext(); CGRect rect = (CGRect){ CGPointZero, self.size }; CGContextSetBlendMode(context, kCGBlendModeNormal); [self drawInRect:rect]; CGContextSetBlendMode(context, kCGBlendModeSourceIn); [tintColor setFill]; CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

我更喜欢不同的解决scheme(无类别),当你设置图像的UISegmentedControl比你必须改变这样的图像:

 NSArray *items = nil; if (NSFoundationVersionNumber>NSFoundationVersionNumber_iOS_6_1) { items = @[ [[UIImage imageNamed:@"Images_Icon_Notes.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal], [[UIImage imageNamed:@"Images_Icon_Keywords.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal], [[UIImage imageNamed:@"Images_Icon_Actionitems.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal], [[UIImage imageNamed:@"Images_Icon_Questions.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] ]; } else { items = @[ [UIImage imageNamed:@"Images_Icon_Notes.png"], [UIImage imageNamed:@"Images_Icon_Keywords.png"], [UIImage imageNamed:@"Images_Icon_Actionitems.png"], [UIImage imageNamed:@"Images_Icon_Questions.png"] ]; } UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items]; segmentedControl.tintColor = [UIColor greenColor]; // desired color of border 

现在tintColor只会影响边框而不是图标。

if提供与旧的iOS版本的兼容性。
我必须说这imageWithRenderingMode:是我见过的最伟大的API跆拳道之一。

什么对我有用:是其他答案build议,将segmentedControl的tintColor更改为clearColor。 并手动色调的图像到你的应用色调的颜色。

记得在着色的图像上使用imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal

 -(UIImage *)tintedImage:(UIImage *)image withColor:(UIColor *)tintColor { UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0); CGContextRef context = UIGraphicsGetCurrentContext(); CGRect rect = (CGRect){ CGPointZero, image.size }; CGContextSetBlendMode(context, kCGBlendModeNormal); [image drawInRect:rect]; CGContextSetBlendMode(context, kCGBlendModeSourceIn); [tintColor setFill]; CGContextFillRect(context, rect); UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } 

您可以先更改tintColor,然后更改titleText。

 //Changes Tint Color segmentedControlName.tintColor = [UIColor colorWithRed:0/255 green:0/255 blue:0/255 alpha:1]; //TitleText to BlackColor [[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] } forState:UIControlStateNormal]; 

我的解决scheme给分段控件的边框另一种颜色,并保持文本为色调的颜色。

为了只更改分段控件的边框颜色,请将另一个分段控件放在旧的控件上。 然后禁用这个新的用户交互,并将选定的段的图像设置为零。

 UISegmentedControl *segCtrl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Segm 1", @"Segm 2", @"Segm 3", @"Segm 4", nil]]; // The UISegmentedController which you want to change the border color for [segCtrl setFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 30)]; [segCtrl setSelectedSegmentIndex:0]; [segCtrl setTintColor:[UIColor redColor]]; UISegmentedControl *bcg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@" ", @" ", @" ", @" ", nil]]; // The UISegmentedController you put on top of the other one [bcg setFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 30)]; [bcg setSelectedSegmentIndex:0]; [bcg setImage:nil forSegmentAtIndex:0]; // Removing highlight color [bcg setTintColor:[UIColor greenColor]]; [bcg setUserInteractionEnabled:NO]; [[self view] addSubview:segCtrl]; [[self view] addSubview:bcg]; 

当然,你必须照顾框架和着色,以适应您的应用程序。 除此之外,这个代码应该是很好的去。

  for (int i=0; i<[mySegmentedControl.subviews count]; i++) { if ([[mySegmentedControl.subviews objectAtIndex:i]isSelected] ) { [[mySegmentedControl.subviews objectAtIndex:i] setBackgroundColor:[UIColor blueColor]]; [[mySegmentedControl.subviews objectAtIndex:i] setTintColor:[UIColor clearColor]]; } else { [[mySegmentedControl.subviews objectAtIndex:i] setBackgroundColor:[UIColor whiteColor]]; [[mySegmentedControl.subviews objectAtIndex:i] setTintColor:[UIColor clearColor]]; } }