环顾四周,试图改变边框颜色(使用不同的文字颜色),没有运气。可以更改色调,但同时更改文本和边框。
答案 0 :(得分:40)
您可以使用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;
}
答案 1 :(得分:8)
我更喜欢不同的解决方案(没有类别),当你为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 WTF之一。
答案 2 :(得分:0)
对我有用:正如其他答案所示,将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];
}
答案 3 :(得分:0)
您可以先更改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];
答案 4 :(得分:-1)
我的解决方案为分段控件的边框提供了另一种颜色,并将文本保持为浅色。
要仅更改分段控件的边框颜色,请将另一个分段控件放在旧分段控件的顶部。然后禁用这个新用户的用户交互,并将所选段的图像设置为nil。
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];
当然,您必须注意框架和着色以适合您的应用。除此之外,这段代码应该不错。
答案 5 :(得分:-2)
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]];
}
}