UISegmentedControl tint color to touch

I have a UISegmentedControl in my application (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 get it to show anything when you click on it ...

It functions fine, but I would like it to be grayed out when you click on it (but only when you click) ... is this possible?

Thank,

Gaultier.

+2


source to share


3 answers


The hue color of the selected segment appears to be getting darker than the original hue. Therefore, if your shade is black, the color of the selected segment shade does not change as there is nothing darker than that.



I looked around and didn't find a nice way to control the hue color of the selected segment for this control.

+2


source


A recipe for creating your own segmented controls that (I think) can be customized the way you want:

idevrecipes custom segmented control



By the way: Very interesting site!

+1


source


Try it. It works if you are using segment headers, you may need to change it if you are using 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];
}

      

0


source







All Articles