简体   繁体   English

如何在uisegmentedcontrol中设置单个段的颜色?

[英]How to set individual segment color in uisegmentedcontrol?

I'm using the following code 我正在使用以下代码

- (void)viewWillAppear:(BOOL)animated
.......


    [[self.controlStatus.subviews objectAtIndex:2]  setTintColor:[UIColor greenColor]];
    [[self.controlStatus.subviews objectAtIndex:1] setTintColor:[UIColor orangeColor]];
    [[self.controlStatus.subviews objectAtIndex:0] setTintColor:[UIColor redColor]];
.........

But this code is not working always . 但是这段代码并不总是有效。 Sometimes the index changes and 1st segment stays green or orange. 有时索引会变化,并且第一段保持绿色或橙色。 I don't know whats happening !! 我不知道发生了什么! Can anyone help me ? 谁能帮我 ?

The UISegmentedControl offers the ability to set the tint color for the entire control, but it does not offer the ability to set the tint color for an individual segment. UISegmentedControl提供了为整个控件设置色调颜色的功能,但没有提供为单个段设置色调颜色的功能。 In fact, the UISegment class, the actual class for the individual segments, is not publicly documented. 实际上,UISegment类(各个细分的实际类)并未公开记录。 So, what I propose here does go a bit off the farm, but it only uses publicly available API to accomplish it. 因此,我在这里提出的建议确实有点过时,但是它仅使用公共可用的API来完成它。 Here is the end result. 这是最终结果。

UISegmentedControlExtension.h UISegmentedControlExtension.h

@interface UISegmentedControl(CustomTintExtension)
-(void)setTag:(NSInteger)tag forSegmentAtIndex:(NSUInteger)segment;
-(void)setTintColor:(UIColor*)color forTag:(NSInteger)aTag;
-(void)setTextColor:(UIColor*)color forTag:(NSInteger)aTag;
-(void)setShadowColor:(UIColor*)color forTag:(NSInteger)aTag;
@end

UISegmentedControlExtension.m UISegmentedControlExtension.m

#import "UISegmentedControlExtension.h"


@implementation UISegmentedControl(CustomTintExtension)

-(void)setTag:(NSInteger)tag forSegmentAtIndex:(NSUInteger)segment {
[[[self subviews] objectAtIndex:segment] setTag:tag];
}

-(void)setTintColor:(UIColor*)color forTag:(NSInteger)aTag {
// must operate by tags.  Subview index is unreliable
UIView *segment = [self viewWithTag:aTag];
SEL tint = @selector(setTintColor:);

// UISegment is an undocumented class, so tread carefully
// if the segment exists and if it responds to the setTintColor message
if (segment && ([segment respondsToSelector:tint])) {
 [segment performSelector:tint withObject:color];
}
}

-(void)setTextColor:(UIColor*)color forTag:(NSInteger)aTag {
UIView *segment = [self viewWithTag:aTag];
for (UIView *view in segment.subviews) {
 SEL text = @selector(setTextColor:);

 // if the sub view exists and if it responds to the setTextColor message
 if (view && ([view respondsToSelector:text])) {
  [view performSelector:text withObject:color];
 }
}
}

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

// you probably know the drill by now
// you could also combine setShadowColor and setTextColor
UIView *segment = [self viewWithTag:aTag];
for (UIView *view in segment.subviews) {
 SEL shadowColor = @selector(setShadowColor:);
 if (view && ([view respondsToSelector:shadowColor])) {
  [view performSelector:shadowColor withObject:color];
 }
}
}

@end

Once that is in place, here is an example of a typical UIViewController taking advantage of it. 一旦到位,下面是一个典型的利用它的UIViewController的示例。

SegmentColorsViewController.m SegmentColorsViewController.m

#import "SegmentColorsViewController.h"
#import "UISegmentedControlExtension.h"

#define kTagFirst 111
#define kTagSecond 112
#define kTagThird 113

@interface SegmentColorsViewController(PrivateMethods)
-(void)segmentChanged:(id)sender;
-(void)setTextColorsForSegmentedControl:(UISegmentedControl*)segmented;
@end

@implementation SegmentColorsViewController

-(void)viewDidLoad {

// create a simple segmented control
// could have done this in Interface Builder just the same
NSArray *items = [[NSArray alloc] initWithObjects:@"orange", @"yellow", @"green", nil];
UISegmentedControl *colors = [[UISegmentedControl alloc] initWithItems:items];
[items release];
[colors setSegmentedControlStyle:UISegmentedControlStyleBar];
[colors setTintColor:[UIColor lightGrayColor]];
[colors setFrame:CGRectMake(20.0f, 20.0f, 280.0f, 30.0f)];
[colors addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:colors];


// ... now to the interesting bits

// at some point later, the segment indexes change, so
// must set tags on the segments before they render
[colors setTag:kTagFirst forSegmentAtIndex:0];
[colors setTag:kTagSecond forSegmentAtIndex:1];
[colors setTag:kTagThird forSegmentAtIndex:2];

[colors setTintColor:[UIColor orangeColor] forTag:kTagFirst];
[colors setTintColor:[UIColor yellowColor] forTag:kTagSecond];
[colors setTintColor:[UIColor greenColor] forTag:kTagThird];

[self setTextColorsForSegmentedControl:colors];
[colors release];
}

-(void)segmentChanged:(id)sender {
// when a segment is selected, it resets the text colors
// so set them back
[self setTextColorsForSegmentedControl:(UISegmentedControl*)sender];
}

-(void)setTextColorsForSegmentedControl:(UISegmentedControl*)segmented {
[segmented setTextColor:[UIColor yellowColor] forTag:kTagFirst];
[segmented setTextColor:[UIColor blackColor] forTag:kTagSecond];
[segmented setTextColor:[UIColor blueColor] forTag:kTagThird];

[segmented setShadowColor:[UIColor redColor] forTag:kTagFirst];
[segmented setShadowColor:[UIColor whiteColor] forTag:kTagSecond];
[segmented setShadowColor:[UIColor clearColor] forTag:kTagThird];
}
@end

I found this solution here 我在这里找到了这个解决方案

Hope this will help you... 希望这个能对您有所帮助...

EDIT : 编辑:

Do this to apply tint color to each segment: 这样做可以为每个细分应用色调颜色:

In viewDidLoad 在viewDidLoad中

[colors setTag:kTagFirst forSegmentAtIndex:0];
[colors setTag:kTagSecond forSegmentAtIndex:1];

on SegmentSelectChange Event 在SegmentSelectChange事件上

if(selectedSegmentIndex == 1)
{
    [colors setTintColor:[UIColor grayColor] forTag:kTagFirst];
    [colors setTintColor:[UIColor grayColor] forTag:kTagSecond];
}

Try this... 尝试这个...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在UISegmentedControl中设置禁用段的背景图像 - How to set background image for disabled segment in UISegmentedControl 如何以编程方式为 UISegmentedControl 中的段设置选定状态 - How to programatically set the selected state for a segment in UISegmentedControl 如何在 UISegmentedControl 中设置选定的段索引? - How to set selected segment index in UISegmentedControl? 自定义UISegmentedControl:如何更改所选段的图像的色调颜色? - Customizing UISegmentedControl: how change tint color of selected segment's image? 如何为 UISegmentedControl 的特定部分设置可访问性标签? - How do I set the accessibility label for a particular segment of a UISegmentedControl? 如何将 UISegmentedControl 和 SwiftUI 形状设置为相同的颜色? - How to set a UISegmentedControl and SwiftUI shape to the same color? Swift 如何为 UISegmentedControl 设置默认文本颜色? - Swift how to set default text color for UISegmentedControl? 不隐藏选择的 UISegmentedControl 的颜色未选择段 - Color unselected segment of UISegmentedControl without hiding selection 更改 UISegmentedControl 中特定段的标题文本颜色? - Change title text color of a specific segment in UISegmentedControl? 如何使UISegmentedControl的选定段不可选择? - How to make UISegmentedControl's selected segment unselectable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM