I have a UISegmentedControl in my app (see code below) :
// --------------- 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];
// ---------------
But I can't make it to show something when you click on it ...
It functionnally works perfectly b开发者_如何学Gout I would like it to tint to gray when you click on it (but just when you click) ... would that be possible ?
Thank you,
Gotye.
Seems as if the selected segment's tint color is made darker than original tint. Therefore if your tint is black then the selected segment's tint color doesn't change since there isn't anything darker than that.
I've looked around and haven't found any nice way to control the selected segment's tint color for this control.
A recipe to create own segmented controls, which (I guess) can be configured as you want:
idevrecipes custom segmented control
BTW: A very very interesting website!
Try this. It works if you use segment titles, you may need to modify it if you use images.
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];
}
精彩评论