Ios6 iOS 6中的分段控制颜色

Ios6 iOS 6中的分段控制颜色,ios6,uisegmentedcontrol,tintcolor,Ios6,Uisegmentedcontrol,Tintcolor,我有一个8段的分段控件。我可以更改整个控件的默认着色颜色,但是我可以为控件中的每个段设置不同的颜色吗?我找到了一个在5.1中使用的教程,其中有一个调用此方法的新类 -(void)setTintColor:(UIColor*)color forTag:(NSInteger)aTag{} 但它在iOS 6中不起作用。有什么想法吗?您可以为每个片段设置不同的片段图像和颜色。对于颜色,您可以使用: //get the subviews of the segmentedcontrol NSArray

我有一个8段的分段控件。我可以更改整个控件的默认着色颜色,但是我可以为控件中的每个段设置不同的颜色吗?我找到了一个在5.1中使用的教程,其中有一个调用此方法的新类

-(void)setTintColor:(UIColor*)color forTag:(NSInteger)aTag{}

但它在iOS 6中不起作用。有什么想法吗?

您可以为每个片段设置不同的片段图像和颜色。对于颜色,您可以使用:

//get the subviews of the segmentedcontrol

NSArray *arri = [segmentedControl subviews];

//change the color of every subview(segment) you have

[[arri objectAtIndex:0] setTintColor:[UIColor redColor]];

[[arri objectAtIndex:1] setTintColor:[UIColor greenColor]];

希望这能解决问题。

您可以为每个片段设置不同的片段图像和颜色。对于颜色,您可以使用:

//get the subviews of the segmentedcontrol

NSArray *arri = [segmentedControl subviews];

//change the color of every subview(segment) you have

[[arri objectAtIndex:0] setTintColor:[UIColor redColor]];

[[arri objectAtIndex:1] setTintColor:[UIColor greenColor]];
希望这能解决问题。

你说得对。。。 iOS 6不支持分段控制的子视图

我有一个选择给你:

CGRect rect = CGRectMake(0, 0, 80, 44);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
                               [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[segment setImage:img forSegmentAtIndex:0];
您需要将核心图形框架添加到项目中

我们可以在索引处为段绘制图像。。。。但如果使用此选项,则无法使用段标题添加文本。您还需要在上面使用的图像“img”上绘制文本。 如果您有其他方法,请与我们分享。

您是对的。。。 iOS 6不支持分段控制的子视图

我有一个选择给你:

CGRect rect = CGRectMake(0, 0, 80, 44);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
                               [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[segment setImage:img forSegmentAtIndex:0];
您需要将核心图形框架添加到项目中

我们可以在索引处为段绘制图像。。。。但如果使用此选项,则无法使用段标题添加文本。您还需要在上面使用的图像“img”上绘制文本。
如果您有其他方法,请与我们分享。

此问题已在此处解决。由于格式问题,我无法粘贴源代码。

编辑:从链接中添加注释和代码,并固定格式~奥利

这是一个棘手的问题。这会奏效的。将代码放在ViewDidDisplay中。那就行了

- (void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear: animated];
    dispatch_async(dispatch_get_main_queue(), ^{
        for (int i = 0 ; i < [segmentControl.subviews count] ; i++)
        {
            if ([[segmentControl.subviews objectAtIndex: i] isSelected] )
            {
                [[segmentControl.subviews objectAtIndex: i] setTintColor: [UIColor blackColor]];
                break;
            }
        }
    }); 
}
-(void)视图显示:(BOOL)动画
{
[超级视图显示:动画];
dispatch\u async(dispatch\u get\u main\u queue()^{
对于(int i=0;i<[segmentControl.subviews计数];i++)
{
如果([[segmentControl.subviews objectAtIndex:i]被选中])
{
[[segmentControl.subviews对象索引:i]setTintColor:[UIColor blackColor]];
打破
}
}
}); 
}

此问题已在此处修复。由于格式问题,我无法粘贴源代码。

编辑:从链接中添加注释和代码,并固定格式~奥利

这是一个棘手的问题。这会奏效的。将代码放在ViewDidDisplay中。那就行了

- (void)viewDidAppear:(BOOL)animated 
{
    [super viewDidAppear: animated];
    dispatch_async(dispatch_get_main_queue(), ^{
        for (int i = 0 ; i < [segmentControl.subviews count] ; i++)
        {
            if ([[segmentControl.subviews objectAtIndex: i] isSelected] )
            {
                [[segmentControl.subviews objectAtIndex: i] setTintColor: [UIColor blackColor]];
                break;
            }
        }
    }); 
}
-(void)视图显示:(BOOL)动画
{
[超级视图显示:动画];
dispatch\u async(dispatch\u get\u main\u queue()^{
对于(int i=0;i<[segmentControl.subviews计数];i++)
{
如果([[segmentControl.subviews objectAtIndex:i]被选中])
{
[[segmentControl.subviews对象索引:i]setTintColor:[UIColor blackColor]];
打破
}
}
}); 
}

这是一个设置红色并与iOS 6兼容的简单解决方案

for ( UIView *segmentView in [segmentedControl subviews] ) {
    if ( [segmentView respondsToSelector:@selector(setTintColor:)] ) {
        [segmentView performSelector:@selector(setTintColor:)
                          withObject:[UIColor redColor]];
    }
}

这是一个设置红色并与iOS 6兼容的简单解决方案

for ( UIView *segmentView in [segmentedControl subviews] ) {
    if ( [segmentView respondsToSelector:@selector(setTintColor:)] ) {
        [segmentView performSelector:@selector(setTintColor:)
                          withObject:[UIColor redColor]];
    }
}

UiSegmentedControl具有影响“tintColor”行为的属性“segmentedControlStyle”(在iOS7中已弃用)

可能的样式有:

UISegmentedControlStylePlain,   
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar,     
UISegmentedControlStyleBezeled, 
但实际上在iOS6中,“Bezeled”(不推荐使用)等于“Bar”

对于前两种样式,如果应用了“tintColor”,则无法进行更改。要对其进行自定义,您需要使用以下方法更改每个片段的图像:

- (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
通过这种方式,您将获得一个完全自定义的分段控件

但是,如果你的设计中有足够的污点,你可以使用这种风格

UISegmentedControlStyleBar

“tintColor”属性将生效,您将获得一个彩色分段控件,该控件将根据所选分段应用该色调,以及允许系统使用它拨号的所有其他好处。

UiSegmentedControl有一个影响“tintColor”行为的属性“segmentedControlStyle”(在iOS7中不推荐使用)

可能的样式有:

UISegmentedControlStylePlain,   
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar,     
UISegmentedControlStyleBezeled, 
但实际上在iOS6中,“Bezeled”(不推荐使用)等于“Bar”

对于前两种样式,如果应用了“tintColor”,则无法进行更改。要对其进行自定义,您需要使用以下方法更改每个片段的图像:

- (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
通过这种方式,您将获得一个完全自定义的分段控件

但是,如果你的设计中有足够的污点,你可以使用这种风格

UISegmentedControlStyleBar

“tintColor”属性将生效,您将获得一个彩色分段控件,该控件将根据所选分段应用色调,并具有让系统使用它拨号的所有其他优点。

嗨,Saalis,这适用于5.1,但不适用于新的6.0。以为这是我的项目,我创建了一个新的空白项目,上面只有一个分段控件,并添加了此代码。它在5.1模拟器上工作得很好,但在6.0模拟器上却不行。有什么想法吗?嗨,Saalis,这适用于5.1,但不适用于新的6.0。以为这是我的项目,我创建了一个新的空白项目,上面只有一个分段控件,并添加了此代码。它在5.1模拟器上工作得很好,但在6.0模拟器上却不行。有什么想法吗?谢谢你在帖子里给出了最好的答案。像这样的定制将有助于从现在起支持ios 7.x和6.x。感谢您提供的最佳答案。像这样的定制将有助于从现在起支持ios 7.x和6.x。我想给+100。我为此浪费了七个小时。在看过你的代码后,它的工作非常好@巴里琼斯。继续攻击我想给+100。我为此浪费了七个小时。在看过你的代码后,它的工作非常好@巴里琼斯。继续黑客攻击