我的应用程序中有一个UISegmentedControl(参见下面的代码):
// --------------- SETTING NAVIGATION BAR RIGHT BUTTONS
NSArray *segControlItems = [NSArray arrayWithObjects:[UIImage imageNamed:@"up.png"],[UIImage imageNamed:@"down.png"], nil];
segControl = [[UISegmentedControl alloc] initWithItems:segControlItems];
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.momentary = YES;
segControl.frame = CGRectMake(25.0, 7, 65.0, 30.0);
segControl.tintColor = [UIColor blackColor];
[segControl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
if (current == 0) [segControl setEnabled:NO forSegmentAtIndex:0];
if (current == ([news count]-1)) [segControl setEnabled:NO forSegmentAtIndex:1];
// ---------------
但是当你点击它时我无法表现出来......
它的功能完美无缺,但是当你点击它时我想要它变成灰色(但是当你点击它时)......这会是可能的吗?
谢谢,
高堤耶。
答案 0 :(得分:2)
似乎所选片段的色调颜色比原始色调更暗。因此,如果你的色调是黑色的,那么所选片段的色调颜色不会改变,因为没有比这更暗的东西。
我环顾四周,并没有找到任何好方法来控制此控件的选定片段的色调颜色。
答案 1 :(得分:1)
创建自己的分段控件的配方,(我猜)可以根据需要进行配置:
BTW:一个非常有趣的网站!答案 2 :(得分:0)
试试这个。如果您使用分段标题,它可以工作,如果您使用图像,则可能需要对其进行修改。
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segControl addTarget:self action:@selector(changeColors:) forControlEvents:UIControlEventValueChanged];
-(IBAction)changeColors:(UISegmentedControl *)sc{
for (int i=0; i<sc.numberOfSegments; i++) {
// reset all the titles in the segmented control
[sc setTitle:[NSString stringWithFormat:@"%i", i] forSegmentAtIndex:i];
}
if (sc.selectedSegmentIndex < 0 || sc.selectedSegmentIndex >= sc.numberOfSegments) {
return;
}
CGFloat width = sc.frame.size.width / sc.numberOfSegments - 1;
CGRect rect = CGRectMake(0, 0, width, sc.frame.size.height - 2);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// background gradient
UIColor *colorOne = [UIColor colorWithWhite:0.6 alpha:1.0]; //top of gradient
UIColor *colorTwo = [UIColor colorWithWhite:0.4 alpha:1.0]; //bottom of gradient
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,rect.size.height), 0);
// black shadow at the top
colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.0 alpha:0.3].CGColor, (id)[UIColor clearColor].CGColor, nil];
gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,2.0), 0);
UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, sc.frame.size.height - 4)];
title.text = [sc titleForSegmentAtIndex:sc.selectedSegmentIndex];
title.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
title.textAlignment = UITextAlignmentCenter;
title.textColor = [UIColor whiteColor];
title.backgroundColor = [UIColor clearColor];
title.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
title.shadowOffset = CGSizeMake(0, -0.5);
[title.layer renderInContext:ctx];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[sc setImage:img forSegmentAtIndex:sc.selectedSegmentIndex];
}