Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在uisegmentedcontrol中设置各个段的颜色?_Ios_Objective C_Iphone_Ipad_Uisegmentedcontrol - Fatal编程技术网

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

Ios 如何在uisegmentedcontrol中设置各个段的颜色?,ios,objective-c,iphone,ipad,uisegmentedcontrol,Ios,Objective C,Iphone,Ipad,Uisegmentedcontrol,我正在使用以下代码 - (void)viewWillAppear:(BOOL)animated ....... [[self.controlStatus.subviews objectAtIndex:2] setTintColor:[UIColor greenColor]]; [[self.controlStatus.subviews objectAtIndex:1] setTintColor:[UIColor orangeColor]]; [[self.contr

我正在使用以下代码

- (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]];
.........
但是这个代码并不总是有效的。有时索引会发生变化,第一段会保持绿色或橙色。
我不知道发生了什么!!有人能帮我吗?

UISegmentedControl提供了为整个控件设置着色颜色的功能,但它不提供为单个片段设置着色颜色的功能。事实上,UISegment类(单个段的实际类)没有公开文档。所以,我在这里提出的建议有点离谱,但它只使用公开可用的API来实现。这是最终结果

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

#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
#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
一旦设置到位,下面是一个典型的UIViewController利用它的示例

SegmentColorsViewController.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
#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
我找到了这个解决方案

希望这能帮助你

编辑:

执行此操作可将着色颜色应用于每个分段:

在viewDidLoad中

[colors setTag:kTagFirst forSegmentAtIndex:0];
[colors setTag:kTagSecond forSegmentAtIndex:1];
关于更改事件

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

-(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]];
        }
    }
}
-(void)setSelectedSegmentColor:(UISegmentedControl*)mySegmentedControl{

对于(int i=0;通常,该段由图片生成,用于打开/关闭状态。希望这些帮助:此代码很好,但我需要为未选择的段之一添加灰色。是否可能!!请参阅
段颜色查看控制器.m
视图加载
。抱歉,我没有搜索此项。例如,如果我选择了index1,所以我想用索引1和索引2更改两个段的颜色。