ios iphone ios7 分段 UISegmentedControl 仅更改边框颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19041035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iphone ios7 segmented UISegmentedControl change only border color
提问by ort11
Been looking around and trying to change just the border color (with a different text color) with no luck. Can change the tint, but changes both the text and border.
一直环顾四周并试图仅更改边框颜色(使用不同的文本颜色),但没有运气。可以更改色调,但同时更改文本和边框。
回答by kkodev
You can use UIAppearance proxy to set title text attributes but preserve tintColor for borders. Something like:
您可以使用 UIAppearance 代理设置标题文本属性,但保留边框的 tintColor。就像是:
[[UISegmentedControl appearance] setTitleTextAttributes:@{
NSForegroundColorAttributeName : [UIColor redColor]
} forState:UIControlStateNormal];
Edit:
编辑:
To tint images, you can use something like this in category on UImage:
要着色图像,您可以在 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;
}
回答by Marek R
I prefer different solution (no category), when you set images for UISegmentedControl
than you have to alter images like that:
我更喜欢不同的解决方案(无类别),当您设置图像时,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
Now tintColor
will have impact only on border not on icons.
现在tintColor
只会影响边框而不影响图标。
if
provides compatibility with older iOS versions.
I must say this imageWithRenderingMode:
is one of the greatest API WTF I've ever seen.
if
提供与旧 iOS 版本的兼容性。
我必须说这imageWithRenderingMode:
是我见过的最伟大的 API WTF 之一。
回答by ugiflezet
What worked for me: is as other answers suggest, change the tintColor of the segmentedControl to clearColor. And manually tint the images to your app tint color.
对我有用的是:正如其他答案所建议的那样,将 segmentedControl 的 tintColor 更改为 clearColor。并手动将图像着色为您的应用程序着色颜色。
Remember to use the imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
on the tinted image
记得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];
}
回答by Victor Rius
You can change first the tintColor and after the titleText.
您可以先更改 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];
回答by OscarWyck
My solution gives the border of the segmented control another color, and keeps the text as the tint color.
我的解决方案为分段控件的边框提供了另一种颜色,并将文本保留为淡色。
In order to only change the border color of your segmented control, put another segmented control on top of your old one. Then disable user interaction for this new one, and I set the image for the selected segment to nil.
为了仅更改分段控件的边框颜色,请将另一个分段控件放在旧控件的顶部。然后禁用这个新的用户交互,我将所选片段的图像设置为 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];
Of course, you have to take care of the framing and coloring to fit your application. Other than that, this code should be good to go.
当然,您必须注意取景和着色以适合您的应用程序。除此之外,这段代码应该很好用。
回答by g212gs
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]];
}
}